Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to understand php foreach list, I'm missing something

This should probably be simple, but I’m missing something.

I want to have two arrays, combine them, then list out the values.

I obviously don’t understand arrays, or array_combine, or the foreach list… but I’m not sure which or where I’m going awry.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s the code:

    <?php

       $media_ids = array('64767','64764');
       $alt_text = array('test 1','test 2');
       
       $img_meta = array_combine($media_ids, $alt_text);
                         
    foreach ($img_meta as list($id, $alt) ) {

        echo $id.' > '.$alt.'<br /><br />';

   }

The results I am looking to end up with would be:

64767 > test 1

64764 > test 2

Any help in accomplishing this would be very appreciated!

Chris

>Solution :

This is how it’s done:

foreach ($img_meta as $id => $alt) {

    echo $id.' > '.$alt.'<br /><br />';

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading