-1

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

  • 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;
}
4

3 回答 3

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;
});

PS代码未经测试,只是一个想法。

于 2013-02-25T06:02:26.257 回答
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));
于 2013-02-25T06:13:54.820 回答
0

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

于 2013-02-25T06:14:24.443 回答