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

Time:05-15

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.

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

CodePudding user response:

This is how it's done:

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

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

}
  • Related