简体   繁体   English

PHP,如何按级别从关联数组中获取值

[英]PHP, How can i get the values from associative array by level

my question is how can i get all values from determined level of some array, i have this array:我的问题是如何从某个数组的确定级别获取所有值,我有这个数组:

array (size=4)
  'Azul' => 
    array (size=2)
      '128GB' => 
        array (size=2)
          'Cristal' => string 'Cristal' (length=7)
          'Plastico' => string 'Plastico' (length=8)
      '64GB' => 
        array (size=1)
          'Cristal' => string 'Cristal' (length=7)
  'Blanco' => 
    array (size=2)
      '32GB' => 
        array (size=1)
          'Plastico' => string 'Plastico' (length=8)
      '64GB' => 
        array (size=1)
          'Madera' => string 'Madera' (length=6)
  'Dorado' => 
    array (size=1)
      '64GB' => 
        array (size=1)
          'Plastico' => string 'Plastico' (length=8)
  'Verde' => 
    array (size=1)
      '64GB' => 
        array (size=1)
          'Madera' => string 'Madera' (length=6)

And i want get the first level with this recursive function, but i cant find more deeper than 2 levels for example i need the first level and i get:我想用这个递归 function 获得第一级,但我找不到比 2 级更深的级别,例如我需要第一级,我得到:

Azul, Blanco, Dorado, Verde Azul、Blanco、Dorado、Verde

But i need the second level of Azul i get: 128GB, 64GB但我需要二级 Azul:128GB、64GB

The questions is, if i need the 3rd level of Azul and 64GB, what can i do to get this, having the keys Azul and 64GB or level 3.问题是,如果我需要 Azul 的第 3 级和 64GB,我该怎么做才能得到它,拥有 Azul 和 64GB 或 3 级的密钥。

My recursive but buggy function is this:我的递归但有问题的 function 是这样的:

function recursive($array, $level, $itemLVL)
    {
        foreach ($array as $key => $value) {
            //If $value is an array.
            if (is_array($value)) {
                //We need to loop through it.
                if ($level == $itemLVL) {
                    //echo "<br> Key: $key - Nivel:$level $itemLVL";
                    echo "<option value='$key'>$key</option>";
                }
                
                recursive($value, $level + 1, $itemLVL);
            } elseif ($level == $itemLVL) {
                echo "<option value='$key'>$key</option>";
            }
        }
    }

If you know the key names and order, then something like this will return what is under those keys:如果你知道键名和顺序,那么像这样的东西将返回这些键下的内容:

function get_array_path($path, $array) {
    $temp =& $array;

    foreach($path as $key) {
        $temp =& $temp[$key];
    }
    return $temp;
}

Pass an array of the keys in order:按顺序传递一组键:

$result = get_array_path(['Azul', '64GB'], $array);

If you just want the keys under the path and not everything else, then pass false as the third argument:如果您只想要路径下的键而不是其他所有内容,则将false作为第三个参数传递:

function get_array_path($path, $array, $values=true) {
    $temp =& $array;

    foreach($path as $key) {
        $temp =& $temp[$key];
    }
    if($values === false) {
        $temp = array_keys($temp);
    }
    return $temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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