简体   繁体   中英

Echo out a foreach Loop

I have this array

 [result] => Array
        (
    [professions] => Array
                (
                    [primary] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 755
                                    [recipes] => Array
                                        (
                                            [0] => 25255
                                            [1] => 25278
                                            [2] => 25280

I need to echo out all the numbers from the [recipes] array, the 25255, 25278...

This is my start:

<ul class="list">';

        foreach ($r['result']['professions']['primary']['0']['recipes'] as $item) echo '

<li>
', $item['???'], '.

</li>';


echo '
                </ul>
</ul>';

Your idea is correct, there are just some syntax issues. It should look more like this:

<ul class="list">

    <?php foreach ($r['result']['professions']['primary'][0]['recipes'] as $item) : ?>
        <li>
            <?= $item ?>
        </li>
    <?php endforeach ?>

</ul>

You are going correctly.Bit modification because syntax are incorrect:-

<ul class="list"> <!-- remove extra '; -->

 <?php foreach ($r['result']['professions']['primary']['0']['recipes'] as $item){
     echo '<li>'.$item.'</li>'; // instead of $item['?'] just use $item and check the concatenation
 ?>
</ul> <!-- only this much is sufficient -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM