phphtmlarraysmarkup

Print row values of a 2d array as HTML markup


I have this 2d array as $array.:

[
    'name' => 'Lorem Ipsum', 'REX_MEDIA_1' => 'wave01.png'],
    'name' => 'Test', 'REX_MEDIA_1' => 'background.jpg'],
]

I would like to format the array-output. For each value there should be something like this:

<div style="background:url(REX_MEDIA_1);"><p>name</p></div>

Which is the best way to do something like that?


Solution

  • You can use a foreach and then print what you want

    The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

    foreach ($array as $key => $value) {
            echo "<div style='background:url(".$value['REX_MEDIA_1'].");'><p>".$value['name']."</p></div>";
        }
    

    Here you have a reference where you can learn more about php loops