sixtillnine
sixtillnine

Reputation: 21

Looping within a Loop from a Nested Array in 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.

Upvotes: 0

Views: 522

Answers (2)

Junaid
Junaid

Reputation: 1300

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>";
}

Upvotes: 1

Hemant Anjana
Hemant Anjana

Reputation: 114

Try this :-

 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>";
}

Keep in mind that your product specifications key must be unique.

Upvotes: 0

Related Questions