PHP数组多级回声

时间:2014-11-10 14:40:41

标签: php arrays

我的数组再次出现问题:( 我有这个:

Array
(
  [results] => Array
    (
        [0] => Array
            (
                [backdrop_path] => htttp://link.com/img.jpg
                [id] => 23951
                [title] => 1
            )

        [1] => Array
            (
                [backdrop_path] => htttp://link.com/img.jpg
                [id] => 23952
                [title] => 2
            )

        [2] => Array
            (
                [backdrop_path] => htttp://link.com/img.jpg
                [id] => 23953
                [title] => 3
            )

        [3] => Array
            (
                [backdrop_path] => htttp://link.com/img.jpg
                [id] => 23954
                [title] => 4
            )
    )

如何将其转换为:

  • title:1
  • title:2
  • title:3
  • title:4
  • 非常感谢你!

    3 个答案:

    答案 0 :(得分:1)

    使用foreach

    echo '<ul>';
    foreach ($array['results'] as $element) {
        echo "<li>title: {$element['title']}</li>";
    }
    echo '</ul>';
    

    答案 1 :(得分:1)

    if (!empty($array['results'])){
       echo '<ul>';
       foreach ($array['results'] as $a){
          echo "<li>title: {$a['title']}</li>";
       }
       echo '</ul>';
    }
    

    答案 2 :(得分:1)

    我建议您使用array_map()代替foreach()for循环。例如:

    echo '<ul>';
    array_map(function($v){echo '<li>Title: '.$v['title'].'</li>';}, $str['results']);
    echo '</ul>';