2

I have an array in the following format.

$data = array(
             1=>array('img'=>'1.png','title'=>'title1','desc'=>'desc1'),
             2=>array('img'=>'2.png','title'=>'title2','desc'=>'desc2'),
             1=>array('img'=>'3.png','title'=>'title3','desc'=>'desc3'),
             );

Here is the final output I need,

<img src="1.png">
<h1>title1</h1>
<p>desc1</p>

<img src="2.png">
<h1>title2</h1>
<p>desc2</p>
 .........

How can i create it? Thanks for the help.

3 Answers 3

3

Use a foreach loop, like so:

foreach( $data as $item) {
    echo '<img src="' . $item['img'] . '">';
    echo '<h1>' . $item['title'] . '</h1>';
    echo '<p>' . $item['desc'] . '</p>';
    echo "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

2

Simply use your array like

  foreach($data as $item){ 
    echo $item['title'];
    .....
  }

This will give you the logic. Now you can apply the img and tags properly

Comments

1
<?php 
        foreach($data as $item) {
        ?>

        <img src="<?=$item['img']?>">
        <h1><?=$item['title']?></h1>
        <p><?=$item['desc']?></p>
        <br />

        <?php } ?>

Another option here.

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.