Jump to content

is this the best way to access a multi array


CyberShot

Recommended Posts

I have this array and I am trying to get the image value

 

print_r($image);

Array
(
    [0] => Array
        (
            [image] => 32
            [image_fallback] => 
        )

    [1] => Array
        (
            [image] => 31
            [image_fallback] => 
        )

)

The only way I have been able to get it to work is by doing this

foreach($image as $key){
   foreach($key as $value){ 
     $imageID = wp_get_attachment_image($value,'full'); // gets the attachment image from the image ID.. 31, 32 from the above array
     echo $imageID; // prints out the image tag with the url to the image
  }
} 

The above code does give me the desired result. What I want to know is if there is a sexier way to getting it done? Is this considered propper?

Link to comment
Share on other sites

I'd guess something like this would work for you.

foreach ($image as $img){
    $imageID = wp_get_attachment_image($img['image'], 'full');
    echo $imageID;
}
In your original code you would run wp_get_attachment_image twice, once for the [image] key and once for the [image_fallback] key which is probably not what you intended to do. If you only want the [image] key then just access it directly.
Link to comment
Share on other sites

What I want to know is if there is a sexier way to getting it done?

 

Using kicken's code as a starting point, this is the 'sexiest' way I can come up with

foreach ($image as $img){
    $imageID = wp_get_attachment_image($img['image'], 'full');
 
 
//               Sexy?Sex
//              ?Sexy?Sexy
//             y?Sexy?Sexy?
//             ?Sexy?Sexy?S
//             ?Sexy?Sexy?S
//            ?Sexy?Sexy?Se
//           ?Sexy?Sexy?Se
//           ?Sexy?Sexy?Se
//           ?Sexy?Sexy?Sexy?
//          ?Sexy?Sexy?Sexy?Sexy
//          ?Sexy?Sexy?Sexy?Sexy?Se
//          ?Sexy?Sexy?Sexy?Sexy?Sex
//           ?Sexy?  ?Sexy?Sexy?Sex
//             ?Sex    ?Sexy?Sexy?
//             ?Sex     ?Sexy?Sexy
//             ?Sex     ?Sexy?Sexy
//              ?Sex    ?Sexy?Sexy
//               ?Se    ?Sexy?Sex
//                ?Se  ?Sexy?Sexy
//                 ?Sexy?Sexy?Sex
//                  ?Sexy?Sexy?sex
//                 ?Sexy?Sexy?Sexy?Se
//                 ?Sexy?Sexy?Sexy?Sexy?
//                 ?Sexy?Sexy?Sexy?Sexy?Sexy
//                 ?Sexy?Sexy?Sexy?Sexy?Sexy?S
//                  ?Sexy?Sexy    ?Sexy?Sexy?se
//                   ?Sexy?Se       ?Sexy?Sexy?
//                   ?Sexy?Se     ?Sexy?Sexy?
//                   ?Sexy?S    ?Sexy?Sexy
//                   ?Sexy?S ?Sexy?Sexy
//                  ?Sexy?Sexy?Sexy
//                  ?Sexy?Sexy?S
//                  ?Sexy?Sexy
//                 ?Sexy?Se
//                 ?Sexy?
//                ?Sexy?
//                ?Sexy?
//                ?Sexy?
//                ?Sexy
//                ?Sexy
//                 ?Sex
//                 ?Sex
//                 ?Sex
//                ?Sexy
//                ?Sexy
//                 Sexy
//                  Sexy?
//                  SexY
 
 
    echo $imageID;
}
Link to comment
Share on other sites

 

Using kicken's code as a starting point, this is the 'sexiest' way I can come up with

foreach ($image as $img){
    $imageID = wp_get_attachment_image($img['image'], 'full');
 
 
//               Sexy?Sex
//              ?Sexy?Sexy
//             y?Sexy?Sexy?
//             ?Sexy?Sexy?S
//             ?Sexy?Sexy?S
//            ?Sexy?Sexy?Se
//           ?Sexy?Sexy?Se
//           ?Sexy?Sexy?Se
//           ?Sexy?Sexy?Sexy?
//          ?Sexy?Sexy?Sexy?Sexy
//          ?Sexy?Sexy?Sexy?Sexy?Se
//          ?Sexy?Sexy?Sexy?Sexy?Sex
//           ?Sexy?  ?Sexy?Sexy?Sex
//             ?Sex    ?Sexy?Sexy?
//             ?Sex     ?Sexy?Sexy
//             ?Sex     ?Sexy?Sexy
//              ?Sex    ?Sexy?Sexy
//               ?Se    ?Sexy?Sex
//                ?Se  ?Sexy?Sexy
//                 ?Sexy?Sexy?Sex
//                  ?Sexy?Sexy?sex
//                 ?Sexy?Sexy?Sexy?Se
//                 ?Sexy?Sexy?Sexy?Sexy?
//                 ?Sexy?Sexy?Sexy?Sexy?Sexy
//                 ?Sexy?Sexy?Sexy?Sexy?Sexy?S
//                  ?Sexy?Sexy    ?Sexy?Sexy?se
//                   ?Sexy?Se       ?Sexy?Sexy?
//                   ?Sexy?Se     ?Sexy?Sexy?
//                   ?Sexy?S    ?Sexy?Sexy
//                   ?Sexy?S ?Sexy?Sexy
//                  ?Sexy?Sexy?Sexy
//                  ?Sexy?Sexy?S
//                  ?Sexy?Sexy
//                 ?Sexy?Se
//                 ?Sexy?
//                ?Sexy?
//                ?Sexy?
//                ?Sexy?
//                ?Sexy
//                ?Sexy
//                 ?Sex
//                 ?Sex
//                 ?Sex
//                ?Sexy
//                ?Sexy
//                 Sexy
//                  Sexy?
//                  SexY
 
 
    echo $imageID;
}

LOL

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.