0

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

1

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>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly, thanks for the pointers. I think I understand the method and should be able to employ this in future.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.