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

时间:2017-06-23 18:02:56

标签: php arrays loops nested-loops

我有一个产品数据数组,在数组中有包含规范数据的嵌套数组。我希望以块的形式返回数据,循环显示每个产品的名称,然后显示它的属性。测试数组的设置如下:

$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',
        ]
    ],
];

我要找的最终结果是:

<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>

我是PHP数组的新手,无法找到有关如何在我理解的循环中执行循环的任何参考。

2 个答案:

答案 0 :(得分:1)

首先,在数组定义中稍作修正。虽然您的数组定义正在运行,但是当您使用关联数组时,让我们充分利用该功能。

在下面的代码中,我删除了数组数据中的变量,它们没用。制作了一个名为specification的新密钥,以便更好地整理数据,并使用唯一的密钥代替Unknown Property,而这些密钥只是在您的情况下被替换

$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',
        ]
    ],
];

现在,要迭代这个嵌套数组,我们可以使用两个foreach循环,如下所示

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 :(得分:0)

试试这个: -

 echo "<ul>";
 foreach ($products as $product) {
     echo "<h4>".$product['name']."</h4>";
     echo "<ul>";
     foreach ($product[0] as $key => $value) {
         echo "<li>".$value."</li>";
     }
     echo "</ul>";
}

请注意,您的产品规格键必须是唯一的。