2009-11-16 5 views
1

후속 값PHP 배열 출력 문제 PHP에서

값을 증가시키는 기능이 초기 값에 기초하여 (* 2) 배열

열 두번 있는가?

$beta = array(
array('5', '1''1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'), 
array('5','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2') 

); 

/*Example: '5' will be '10' (5*2 =10 and output 10 to web) 
     '2' will be '4' (2*2 = 4 and output 4 to web) 
The next '2' will be '16' (4*2 = 8 and output 8 to web) 
The next '2' will be '32' (8*2 = 16 and output 16 to web) 
And so forth? */ 

내가 단단히 멍청한 놈 다시 감사를 이해하지 않도록 구조의 측면에서 너무 복잡 뭔가가 믿고 있지만 원인 또한이 배열을 구성 할 수있는 쉬운 방법이있다.

[면책 조항 : 3 일 동안 배열을 이해하려고 노력했지만, 이제는 이해할 수 있습니다. 그러나, 나는 여전히 새롭고 현재 배열에있는 값을 조작하여 웹에 출력하려고 할 때 몇 가지 문제가 있습니다. 그리고 나는 아직도 많이 읽고 배워야 할 것이라고 확신합니다. 도움이 필요,이 C에서이 문제를 발견 ++ 책 :

[http://books.google.com/books?id=4Fn_P7tdOZgC&pg=PT424&lpg=PT424&dq=subsequent+++column+is+twice+the+value&source=bl&ots=gSvQ_LhxoI&sig=dG_Ilf1iLO86lqX936cT1PpkPc8&hl=en&ei=OEEBS_eODYyotgOFtJD3CQ&sa=X&oi=book_result&ct=result&resnum=1&ved=0CAgQ6AEwAA#v=onepage&q=subsequent%20%20%20column%20is%20twice%20the%20value&f=false][1]

+0

첫 번째 하위 배열에 쉼표가 누락되었습니다. ;) – MitMaro

답변

1

다음은이 섹션의 각 질문에 대한 답변입니다.

<?php 

// Declare an array alpha of 10 rows and 20 columns of type int 
// Initialise the array alpha to 0 
$alpha = array(array()); 
for($i = 0; $i < 10; $i++) 
{ 
    for($j = 0; $j < 20; $j++) 
    { 
     $alpha[$i][$j] = 0; 
    } 
} 

// Store 1 in the first row and 2 in the remaining rows 
for($i = 0; $i < 10; $i++) 
{ 
    for($j = 0; $j < 20; $j++) 
    { 
     if($i == 0) 
     { 
      $alpha[$i][$j] = 1; 
     } 
     else 
     { 
      $alpha[$i][$j] = 2; 
     } 
    } 
} 

// Store 5 in the first column, and make sure that the value in 
// each subsequent column is twice the value in the previous column 
// (Beware this doesn't build off the initial value of 5 in the first 
// column but the previously set values above) 
for($i = 0; $i < 10; $i++) 
{ 
    for($j = 0; $j < 20; $j++) 
    { 
     if($j == 0) 
     { 
      $alpha[$i][$j] = 5; 
     } 
     else 
     { 
      if($j - 1 >= 1) 
      { 
       $alpha[$i][$j] = $alpha[$i][$j-1] * 2; 
      } 
     } 
    } 
} 

// Print the array alpha one row per line 
print "Printing the array alpha one row per line:<br/>"; 
for($i = 0; $i < 10; $i++) 
{ 
    for($j = 0; $j < 20; $j++) 
    { 
     print "[". $alpha[$i][$j] ."] "; 
    } 

    print "<br/>"; 
} 

print "<br/>"; 

// Print the array alpha one column per line 
print "Printing the array alpha one column per line:<br/>"; 
for($j = 0; $j < 20; $j++) 
{ 
    for($i = 0; $i < 10; $i++) 
    { 
     print "[". $alpha[$i][$j] ."] "; 
    } 

    print "<br/>"; 
} 

?> 
+0

의견을 보내 주셔서 감사합니다. – Newb

3

당신은 array_map을 시도 할 수 있습니다 : 배열을 구성에 관해서는

<?php 
function increase($n) { 
    return is_array($n) ? array_map('increase', $n) : $n * 2; 
} 

$new_beta = array_map("increase", $beta); 

,하지만 난 믿고 그렇게 할 수있는 다른 방법이 있습니다 이것은 가장 구멍이 뚫리고 깨끗합니다.

+0

array_map에 대해 읽을 것이고 나는 array_map에 대한 정보를 처음 듣고 코드 스 니펫을 시도 할 것이다. – Newb

+0

당신은 "당신"이라고 생각합니다, 미안합니다. QWERTY. – Newb