doumanshan6314 2018-10-08 13:44 采纳率: 0%
浏览 44
已采纳

PHP循环遍历数组内的数组

Im trying to loop through an array that is made from a couple of arrays that each has 2 properties and another array with diffrent number of properties. im trying to output all the properties of the last array using foreach.

 $gallery = array(
    array(
        'title' => 'HaPartizanim',
        'file' => './assets/cubes/HaPartizanim.png',
        $slides => array(
            'slide1' => './assets/pop-up/HaPartizanim-1.png',
            'slide2' => './assets/pop-up/HaPartizanim-2.png',
            'slide3' => './assets/pop-up/HaPartizanim-3.png',
            'slide4' => './assets/pop-up/HaPartizanim-4.png'
        )
    ),
    array(
        'title' => 'Toro House',
        'file' => './assets/cubes/Toro_House.png',
        $slides => array(
            'slide1' => './assets/pop-up/Toro_House-1.png',
            'slide2' => './assets/pop-up/Toro_House-2.png',
            'slide3' => './assets/pop-up/Toro_House-3.png'
        )
    ),
    array(
        'title' => 'HaAgana',
        'file' => './assets/cubes/HaAgana.png',
        $slides => array(
            'slide1' => './assets/pop-up/HaAgana-1.png',
            'slide2' => './assets/pop-up/HaAgana-2.png',
            'slide3' => './assets/pop-up/HaAgana-3.png',
            'slide4' => './assets/pop-up/HaAgana-4.png'
        )
    ),
    array(
        'title' => 'Har HaCarmel',
        'file' => './assets/cubes/Har_HaCarmel.png',
        $slides => array(
            'slide1' => './assets/pop-up/Har_HaKarmel-1.png',
            'slide2' => './assets/pop-up/Har_HaKarmel-2.png',
            'slide3' => './assets/pop-up/Har_HaKarmel-3.png',
            'slide4' => './assets/pop-up/Har_HaKarmel-4.png',
        )
    ),
    array(
        'title' => 'Kohvei Itzhak',
        'file' => './assets/cubes/Kohvei_Itzhak.png',
        $slides => array(
            'slide1' => './assets/pop-up/Kohvei_Itzhak-1.png',
            'slide2' => './assets/pop-up/Kohvei_Itzhak-2.png'
        )
    )
);

what i have so far and is not working is

foreach($gallery as $key => $slides){
  foreach($slides as $key => $slide){
            $slide1 = $slide['slide1'];
            $slide2 = $slide['slide2'];
    echo $slide1, $slide2 . "<br/>";
  }
}   

Thanks for the help and sorry for bad english.

  • 写回答

1条回答 默认 最新

  • drtoaamk20278 2018-10-08 13:49
    关注

    First of all, $slides => array( is invalid syntax. I will assume you actually meant 'slides' => array( - if that's true, the following code will list all of the properties from each array using implode().

    foreach($gallery as $key => $slides){
        echo implode(', ', $slides['slides']) . "<br>";
    }
    

    If you want to do additional processing for each slide, loop through like this:

    foreach($gallery as $key => $slides){
        foreach($slides['slides'] as $sub_key => $slide) {
            echo "<div>Key: $sub_key<br>Slide: $slide</div>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆