0

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?

1
  • 2
    foreach loop is your "friend" Commented Oct 17, 2017 at 9:18

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

You'd want to loop through the array:

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

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.