Jump to content

is this the best way to access a multi array


Go to solution Solved by kicken,

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?

  • Solution

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.
  • Like 1

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;
}
  • Like 2

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.