doufen1933 2017-06-23 18:02
浏览 34
已采纳

在PHP中嵌套数组的循环内循环

I have an array of product data, within the array there are nested arrays containing specification data. I wish to return the data in blocks, looping through each product displaying it's name followed by it's properties. The test array is set up as follows:

$products = [
    $product = [
        'name' => 'product name 1a',
        $specification = [
            'Unknown Property' => 'value1a',
            'Unknown Property' => 'value2a',
            'Unknown Property' => 'value3a',
            'Unknown Property' => 'value4a',
        ]
    ],
    $product = [
        'name' => 'product name 1b',
        $specification = [
            'Unknown Property' => 'value1b',
            'Unknown Property' => 'value2b',
            'Unknown Property' => 'value3b',
        ]
    ],
    $product = [
        'name' => 'product name 1c',
        $specification = [
            'Unknown Property' => 'value1c',
            'Unknown Property' => 'value2c',
            'Unknown Property' => 'value3c',
            'Unknown Property' => 'value4c',
        ]
    ],
];

The end result I am looking for is:

<li>
    <h4>Product Name 1a</h4>
    <ul>
        <li>Unknown Property: value1a</li>
        <li>Unknown Property: value2a</li>
        <li>Unknown Property: value3a</li>
        <li>Unknown Property: value4a</li>
    </ul>
</li>
<li>
    <h4>Product Name 1b</h4>
    <ul>
        <li>Unknown Property: value1b</li>
        <li>Unknown Property: value2b</li>
        <li>Unknown Property: value3b</li>
    </ul>
</li>
<li>
    <h4>Product Name 1c</h4>
    <ul>
        <li>Unknown Property: value1c</li>
        <li>Unknown Property: value2c</li>
        <li>Unknown Property: value3c</li>
        <li>Unknown Property: value4c</li>
    </ul>
</li>

I'm new to PHP arrays and cannot find any references on how to perform a loop within a loop that I understand.

  • 写回答

2条回答 默认 最新

  • douyou1857 2017-06-23 18:17
    关注

    First of all, a little correction in your array definition. Although your array definition is working but as you're using associative arrays then let's take full benefit of that feature.

    In the code below, I removed the variables inside array data, they were useless. Made a new key named specification to better organise the data and used unique keys instead of Unknown Property which was just replaced in your case

    $products = [
        [
            'name' => 'product name 1a',
            'specification' => [
                'Unknown Property 1' => 'value1a',
                'Unknown Property 2' => 'value2a',
                'Unknown Property 3' => 'value3a',
                'Unknown Property 4' => 'value4a',
            ]
        ],
        [
            'name' => 'product name 1b',
            'specification' => [
                'Unknown Property 1' => 'value1b',
                'Unknown Property 2' => 'value2b',
                'Unknown Property 3' => 'value3b',
            ]
        ],
        [
            'name' => 'product name 1c',
            'specification' => [
                'Unknown Property 1' => 'value1c',
                'Unknown Property 2' => 'value2c',
                'Unknown Property 3' => 'value3c',
                'Unknown Property 4' => 'value4c',
            ]
        ],
    ];
    

    Now, to iterate through this nested array, we can use two foreach loops as I did below

    foreach ($products as $product) {
        echo "<li>";
        echo "<h4>" . $product[ 'name' ] . "</h4>";
    
        if ( count( $product[ 'specification' ] ) ) {
            // check if there's anything in specification
            // then
            echo "<ul>";
            foreach ($product[ 'specification' ] as $property => $value) {
                echo "<li>" . $property . ":" . $value .  "</li>";
            }
            echo "</ul>";
        }
        echo "</li>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势