2009-11-16 95 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天的時間來理解數組,我現在明白了;然而,我仍然是新手,目前在嘗試操作數組中的值並將其輸出到網絡時遇到了一些問題。而且我仍然確信我有很多需要閱讀和學習的內容,所以請不要使用flamers,我只是需要一些幫助,找到了這款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 2009-11-16 12:25:11

回答

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

感謝您的評論,Ambrosia。 – Newb 2009-11-16 15:31:08

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 2009-11-16 15:33:10

+0

假設你是「你的」,對不起該死的QWERTY。 – Newb 2009-11-16 15:33:57