Get value of element in Multi-Dimensional Array

Posted by George on Stack Overflow See other posts from Stack Overflow or by George
Published on 2014-08-23T21:16:10Z Indexed on 2014/08/23 22:20 UTC
Read the original article Hit count: 258

Filed under:
|

Here is my foreach loop to get at the values from a multi-dimensional array

$_coloredvariables = get_post_meta( $post->ID, '_coloredvariables', true );
foreach ($_coloredvariables as $key => $value) {
   var_dump($value);
}

Which outputs this:

array
  'label' => string 'Color' (length=5)
  'size' => string 'small' (length=5)
  'displaytype' => string 'square' (length=6)
  'values' => 
    array
      'dark-night-angel' => 
        array
          'type' => string 'Image' (length=5)
          'color' => string '#2c4065' (length=7)
          'image' => string '' (length=0)
      'forest-green' => 
        array
          'type' => string 'Color' (length=5)
          'color' => string '#285d5f' (length=7)
          'image' => string '' (length=0)
      'voilet' => 
        array
          'type' => string 'Color' (length=5)
          'color' => string '#6539c9' (length=7)
          'image' => string '' (length=0)
      'canary-yellow' => 
        array
          'type' => string 'Color' (length=5)
          'color' => string 'grey' (length=4)
          'image' => string '' (length=0)

And then to only get the values array I can do this:

foreach ($_coloredvariables as $key => $value) {
    var_dump($value['values']);
}

which outputs this:

array
  'dark-night-angel' => 
    array
      'type' => string 'Image' (length=5)
      'color' => string '#2c4065' (length=7)
      'image' => string '' (length=0)
  'forest-green' => 
    array
      'type' => string 'Color' (length=5)
      'color' => string '#285d5f' (length=7)
      'image' => string '' (length=0)
  'voilet' => 
    array
      'type' => string 'Color' (length=5)
      'color' => string '#6539c9' (length=7)
      'image' => string '' (length=0)
  'canary-yellow' => 
    array
      'type' => string 'Color' (length=5)
      'color' => string 'grey' (length=4)
      'image' => string '' (length=0)

What I can't figure out is how to get these elements in the array structure

"dark-night-angel", "forest-green", "voilet", "canary-yellow"

Without using specific names:

var_dump($value['values']['dark-night-angel'])

Something that is more dynamic, of course this doesn't work:

var_dump($value['values'][0][0]);

thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays