2009-11-16 93 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