0
$shortcodes['video_section'] = array(
    'no_preview' => true,
    'params' => 'xxx',
    'shortcode' => '[sc1][/sc1]',
    'popup_title' => __('Video Section', THEME_NAME),
    'shortcode_icon' => __('li_video')
);

$shortcodes['image_section'] = array(
    'no_preview' => true,
    'params' => 'yyy',
    'shortcode' => '[sc2][/sc2]',
    'popup_title' => __('Image Section', THEME_NAME),
    'shortcode_icon' => __('li_image')
);

$shortcodes[] = $th_shortcodes;

How can I retrieve the name of each array and then access to the key and value:

for example I need to loop throught $shortcode and get the array main name: 'image_section' 'video_section'

Then to retrieve the value of some key. I know how to retrieve key and value but really don't understand how to get the name of the declared array. If i do: var_dump($value); I saw the name of the array but how to access to it?

8
  • 1
    foreach($shortcodes as $key => $value){ echo $key; }? $key = "video_section" / "image_section". Commented Aug 3, 2014 at 17:26
  • 1
    @putvande; you have forgotten something perhaps. Commented Aug 3, 2014 at 17:26
  • I only get with this the $key and value but not the name of the array... Commented Aug 3, 2014 at 17:27
  • it's not "name", it's called "key". Commented Aug 3, 2014 at 17:28
  • the hell do you mean as name, is this the variable name? Commented Aug 3, 2014 at 17:29

4 Answers 4

1

You can use foreach

foreach($shortcodes as $key => $value) {
  echo $key // echoes "vide_section" and "image_section" 

  foreach($value as $innerKey => $innerValue) {
    echo $innerKey // echoes 'no_preview', 'params', 'shortcode', 'popup_title', 'shortcode_icon' twice
  }
}

Note that $value in this case refers to arrays, you can foreach again to access the inner values.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you but it don't work... I get all the array keys and values, not the name of each array
What do you mean by "name"? Arrays don't have names, they have keys (like 'vide_section' and 'image_section'). If you want to get the key names inside the inner arrays you have to foreach again.
It works but it's the $innerKey that output the key of each array... So I need 3 loop to reach child keys of each array.... strange
If you know the outer key names you can do foreach(shortcodes['video_section'] as $innerKey => $innerValue) and foreach(shortcodes['image_section'] as $innerKey => $innerValue), but in this case if you add a third array you will have to write that too as foreach.
In fact I don't now the key name. I find why I need 2 loops before to get my key name. It was because of this : $shortcodes[] = $th_shortcodes; . Only need this $shortcodes = $th_shortcodes;
0

Use the array_flip function on the array

$array = array_flip($shortcodes);

1 Comment

To use array_flip you need to ensure that all the values are unique strings or numbers. Note that this is not the case here, since all the values are arrays.
0

Yes you can use foreach, where it goes through each item on array, based on creation order.

foreach($shortcodes as $key => $value) {

   // $key represents video_section for 1st iteration and image_section for 2nd.
   //Here each are array, so you can again iterate over $value and get each item .

   foreach($value as $key2 => $value2) {
      //here keys are no_preview, params and so on on subsequent iterations.
   }
}

Comments

0

There's array_keys():

$keynames = array_keys($shortcodes);

You could foreach throught it:

foreach($shortcodes as $key=>values){ echo $key; }

or flip the values with the case (though this last one I don't recommend), and than foreach through those. But array flip is best to be left alone in this case (though I nice function to keep in the back of your head).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.