从二维数组中获取值

时间:2015-04-18 02:50:59

标签: php multidimensional-array

代码

$two[0][0]=100;
$two[1][0]=110;
$two[2][0]=120;

var_dump($two);

echo "<br> two[0][0]=$two[0][0]";
echo "<br> two[1][0]=$two[1].0";
echo "<br> two[2][0]=$two[2]";

输出

array (size=3)
  0 => 
    array (size=1)
      0 => int 100
  1 => 
    array (size=1)
      0 => int 110
  2 => 
    array (size=1)
      0 => int 120

( ! ) Notice: Array to string conversion 

two[0][0]=Array[0]
two[1][0]=Array.0
two[2][0]=Array

如何从这个二维数组中获取值?我可以使用echo $single[1]

将get输出用于单维数组

2 个答案:

答案 0 :(得分:1)

使用echo "<br> two[0][0]=$two[0][0]";

它读为$two[0]. "[0]",返回输出为Array[0]

尝试

echo "<br> two[0][0]=" . $two[0][0]; 

echo "<br> two[0][0]={$two[0][0]}";

将您的代码更改为

$two[0][0]=100;
$two[1][0]=110;
$two[2][0]=120;

var_dump($two);

echo "<br> two[0][0]={$two[0][0]}";
echo "<br> two[1][0]={$two[1][0]}";
echo "<br> two[2][0]={$two[2][0]}";

答案 1 :(得分:0)

尝试改变这种方式,

$two[0][0]=100;
$two[1][0]=110;
$two[2][0]=120;

echo "<pre>";
print_r($two);
echo "</pre>";

echo "\n";
//concatenate your 2d array with string here
echo "two[0][0]=".$two[0][0]."\n"; 
echo "two[1][0]=".$two[1][0]."\n";
echo "two[2][0]=".$two[2][0]."\n";