从“多维数组中的特定键”获取值并放入数组中

时间:2013-02-25 05:53:32

标签: php arrays multidimensional-array

我有一个带有转发器的表单创建的大型数组,因此数组可以无限大。我需要:

  • 1。通过数组搜索
  • 2。获取[field_18]
  • 的所有值
  • 3。输出一个包含所有值('-5',' - 1,ect)
  • 的数组
Array
(
[field_22] => Mid-Distance Free
[field_25] => Array
    (
        [0] => 1
    )

[field_23] => Its a freestyle workout
[field_6] => Array
    (
        [0] => Array
            (
                [field_7] => 1
                [field_8] => 0
                [field_9] => 0
                [field_10] => Array
                    (
                        [0] => Array
                            (
                                [field_11] => 6
                                [field_12] => 50
                                [field_13] => jumping jacks
                                [field_14] => 
                            )

                        [1] => Array
                            (
                                [field_11] => 2
                                [field_12] => 20
                                [field_13] => pushups
                                [field_14] => 
                            )

                        [acfcloneindex] => Array
                            (
                                [field_11] => 1
                                [field_12] => 1
                                [field_13] => pushups
                                [field_14] => 
                            )

                    )

            )

        [acfcloneindex] => Array
            (
                [field_7] => 1
                [field_8] => 0
                [field_9] => 0
                [field_10] => Array
                    (
                        [acfcloneindex] => Array
                            (
                                [field_11] => 1
                                [field_12] => 1
                                [field_13] => pushups
                                [field_14] => 
                            )

                    )

            )

    )

[field_17] => Array
    (
        [0] => Array
            (
                [field_7] => 1
                [field_8] => 0
                [field_9] => 0
                [field_10] => Array
                    (
                        [0] => Array
                            (
                                [field_11] => 5
                                [field_12] => 100
                                [field_13] => Freestyle
                                [field_14] => Descend 1-5
                                [field_18] => 5
                            )

                        [1] => Array
                            (
                                [field_11] => 10
                                [field_12] => 50
                                [field_13] => Drill
                                [field_14] => Choice
                                [field_18] => 5
                            )

                        [2] => Array
                            (
                                [field_11] => 1
                                [field_12] => 400
                                [field_13] => Freestyle
                                [field_14] => Negative Split
                                [field_18] => -20
                            )

                        [acfcloneindex] => Array
                            (
                                [field_11] => 1
                                [field_12] => 25
                                [field_13] => Butterfly
                                [field_14] => 
                                [field_18] => 
                            )

                    )

            )

        [acfcloneindex] => Array
            (
                [field_7] => 1
                [field_8] => 0
                [field_9] => 0
                [field_10] => Array
                    (
                        [acfcloneindex] => Array
                            (
                                [field_11] => 1
                                [field_12] => 25
                                [field_13] => Butterfly
                                [field_14] => 
                                [field_18] => 
                            )

                    )

            )

    )

[field_19] => Array
    (
        [0] => Array
            (
                [field_7] => 1
                [field_8] => 0
                [field_9] => 1
                [field_10] => Array
                    (
                        [0] => Array
                            (
                                [field_11] => 20
                                [field_12] => 100
                                [field_13] => Freestyle
                                [field_14] => Hold 500 race pace
                                [field_18] => -5
                            )

                        [1361771245813] => Array
                            (
                                [field_11] => 1
                                [field_12] => 75
                                [field_13] => Butterfly
                                [field_14] => 
                                [field_18] => 
                            )

                        [acfcloneindex] => Array
                            (
                                [field_11] => 1
                                [field_12] => 125
                                [field_13] => Butterfly
                                [field_14] => 
                                [field_18] => 
                            )

                    )

            )

        [acfcloneindex] => Array
            (
                [field_7] => 
                [field_8] => 0
                [field_9] => 0
                [field_10] => Array
                    (
                        [acfcloneindex] => Array
                            (
                                [field_11] => 1
                                [field_12] => 25
                                [field_13] => Butterfly
                                [field_14] => 
                                [field_18] => 
                            )

                    )

            )

    )

[field_21] => Array
    (
        [acfcloneindex] => Array
            (
                [field_7] => 
                [field_8] => 0
                [field_9] => 0
                [field_10] => Array
                    (
                        [acfcloneindex] => Array
                            (
                                [field_11] => 
                                [field_12] => 25
                                [field_13] => Butterfly
                                [field_14] => 
                                [field_18] => 
                            )

                    )

            )

    )

)

我试图通过所有的钥匙,但无法弄清楚从哪里去。这是我正在使用的代码。

function array_keys_multi(array $array)
{
$keys = array();

foreach ($array as $key => $value) {
    $keys[] = $key;

    if (is_array($array[$key])) {
        $keys = array_merge($keys, array_keys_multi($array[$key]));
    }
}

return $keys;
}

3 个答案:

答案 0 :(得分:0)

您可以使用array_walk_recursive

$array = array(/*some data*/);
$result = array();
$search_key = 'field_18';
array_walk_recursive($array, function($item, $key)use($search_key, &$result){
if ($search_key == $key)
  $result[]=$item;
});
$search_values = array('-5', '-1');
$result2 = array();
array_walk_recursive($array, function($item, $key)use($search_values, &$result2){
if (is_array($item) && array_intersect($item, $search_values))
  $result2[]=$item;
});

P.S。代码没有经过测试,只是一个想法。

答案 1 :(得分:0)

试试这个:

function array_key_exists_r($needle, $haystack)
{
    //$result  = array();
    global $result;
    if(is_array($haystack)){
        foreach ($haystack as $key=>$v) {
            if (is_array($v)) {
                array_key_exists_r($needle, $v);
            }
            else{
                if($key == $needle){
                   $result[] = $v;
                }
            }
        }
    }

    return $result;
}


echo "<pre>";
print_r(array_key_exists_r("field_18", $your_array));

答案 2 :(得分:0)

由于您声称使用array_walk可能会非常危险,因此您的数组可能非常大。如果您使用的是PHP 5.5,则可以构建一个简单的foreach循环并生成相应的值。这样,消费函数可以根据需要获取尽可能多的条目。