如何在php中显示多维数组内的值

时间:2014-09-10 06:27:39

标签: php arrays multidimensional-array

我想在这个数组中获取id和name的值。

Array
(
[data] => Array
    (
        [0] => Array
            (
                [id] => 238345159549706
                [members] => Array
                    (
                        [data] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 100001130889528
                                        [name] => Sy Cheeze
                                    )
                                [1] => Array
                                    (
                                        [id] => 100002616426665
                                        [name] => Chun Jenny
                                    )
                                    .......

我尝试过使用这个foreach。

  foreach ($acquaintances as $acquaintance)
    {
      foreach ($acquaintance as $acquaint)
      {
        $acqID = $acquaint['id'];
        $acqName = $acquaint['name'];

        echo $acqName;
      }
    }

但不会显示任何内容。我的代码怎么办?请提出任何想法和建议。谢谢!

2 个答案:

答案 0 :(得分:1)

$array = array
   (
   array("bla",22,18),
   array("blaa",15,13),
   array("blaaa",5,2),
   array("blaaaa",17,15)
   );

for ($row = 0; $row <  4; $row++) {
   echo "<p><b>Row number $row</b></p>";
   echo "<ul>";
   for ($col = 0; $col <  3; $col++) {
     echo "<li>".$array[$row][$col]."</li>";
   }
   echo "</ul>";
}

答案 1 :(得分:1)

您也可以直接在foreach循环中访问索引。像这样:

foreach($acquaintances['data'] as $acquaintance) {
    foreach($acquaintance['members']['data'] as $acquaint) {
        $acqID = $acquaint['id'];
        $acqName = $acquaint['name'];

        echo $acqName . '<br/>';
    }
}