简体   繁体   中英

Get value of element in Multi-Dimensional Array

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

You can add a foreach-loop in your foreach-loop.

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

For 1 $value (so probably inside that foreach):

$colors = array_keys($value['values']);

If you want all the colors in all the values in $_coloredvariables , you'll need that foreach:

$colors = array();
foreach ($_coloredvariables as $key => $value) {
  $colors = array_merge($colors, array_keys($value['values']));
}

This ONLY returns the color keys. NOT the contents of those elements ( type , color & image ).

Is that what you need..?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM