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 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加