简体   繁体   English

如何从多维数组按键值获取数组数据?

[英]How to get Array data by key value from multidimensional array?

I have a multidimensional array in my php variable like, 我在我的php变量中有一个多维数组,例如,

Array
(
    [type] => salesrule/rule_condition_combine
    [attribute] => 
    [operator] => 
    [value] => 1
    [is_value_processed] => 
    [aggregator] => any
    [conditions] => Array
        (
            [0] => Array
                (
                    [type] => salesrule/rule_condition_combine
                    [attribute] => 
                    [operator] => 
                    [value] => 1
                    [is_value_processed] => 
                    [aggregator] => all
                    [conditions] => Array
                        (
                            [0] => Array
                                (
                                    [type] => salesrule/rule_condition_address
                                    [attribute] => postcode
                                    [operator] => >
                                    [value] => 00999
                                    [is_value_processed] => 
                                )

                            [1] => Array
                                (
                                    [type] => salesrule/rule_condition_address
                                    [attribute] => postcode
                                    [operator] => <
                                    [value] => 96200
                                    [is_value_processed] => 
                                )
                        )
                )
            [1] => Array
                (
                    [type] => salesrule/rule_condition_combine
                    [attribute] => 
                    [operator] => 
                    [value] => 1
                    [is_value_processed] => 
                    [aggregator] => all
                    [conditions] => Array
                        (
                            [0] => Array
                                (
                                    [type] => salesrule/rule_condition_address
                                    [attribute] => postcode
                                    [operator] => >=
                                    [value] => 97000
                                    [is_value_processed] => 
                                )

                            [1] => Array
                                (
                                    [type] => salesrule/rule_condition_address
                                    [attribute] => postcode
                                    [operator] => <
                                    [value] => 99500
                                    [is_value_processed] => 
                                )
                        )
                )
        )
)

Here, the length of the array can very. 在这里,数组的长度可以很长。 Now I want to get only postcode values from my array like, 现在,我只想从数组中获取邮政编码值,例如,

Array(
     [0]=>Array
          ( 
             [0] => Array
                 (
                    [attribute] => postcode
                    [operator] => >
                    [value] => 00999
                  )
             [1] => Array
                 (
                    [attribute] => postcode
                    [operator] => <
                    [value] => 96200
                  )
          )
     [1]=>Array
          ( 
             [0] => Array
                 (
                    [attribute] => postcode
                    [operator] => >=
                    [value] => 97000
                  )
             [1] => Array
                 (
                    [attribute] => postcode
                    [operator] => <
                    [value] => 99500
                  )
          )
    )

I have user multiple loops and conditions to do it. 我有多个循环和条件来执行此操作。

foreach( $conditions['conditions'] as $_conditions ):
    foreach( $_conditions['conditions'] as $_condition ):
        $counter++;
        $loopCounter++; 
        foreach($_condition['conditions'] as $condition ):
            if($condition['attribute'] == 'postcode'){ $postcodes[$counter][$condition['operator']] = $condition['value']; }
            $loopCounter++;
        endforeach;
    endforeach;
endforeach;

This works, but I think that is not a proper way to do it. 这行得通,但是我认为这不是正确的方法。 I also tried to use array_map() and array_column() and some other ways but they only works if we have one level of sub arrays. 我还尝试使用array_map()array_column()以及其他一些方式,但是它们仅在我们具有一级子数组的情况下才有效。 Basically I just want to get postcode data from this array without using multilevel loop. 基本上,我只想从此数组中获取邮政编码数据,而不使用多级循环。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Required: Extract certain leaf nodes identified as 'postcode' from a tree held as an array. 必需:从以数组形式保存的tree提取某些标识为“邮政编码”的leaf nodes

There is one issue: 有一个问题:

1) The input array 'root' is an associated array (the 'condition') rather than an array of 'conditions' like the rest of the tree. 1)输入数组“ root”是一个关联的数组(“ condition”),而不是像树的其余部分一样的“ conditions”数组。 This explains why I used the array($sourceData) rather than $sourceData, to start the processing. 这说明了为什么我使用array($ sourceData)而不是$ sourceData来开始处理。

Demonstration at evel.in Source code at Pastebin.com evel.in上的演示Pastebin.com上的 源代码

The function that scans the array 扫描数组的功能

/**
 * function recurse the array looking for post codes...
 *
 * @param array $conditions
 * @param array $postcodes by reference
 * @return void
 */
function getPostcodes($conditions, &$postcodes) {

    $currentPostcodes = array();

    foreach ($conditions as $condition) {

        if (!empty($condition['conditions'])) { // recurse...
           getPostcodes($condition['conditions'], $postcodes);
           continue; // process next entry...
        }

        // extract the postcodes...  
        if (   isset($condition['attribute'])
             && $condition['attribute']  === 'postcode') {
            $currentPostcodes[] = $condition;
        }
    }

    if (!empty($currentPostcodes)) {
        $postcodes[] = $currentPostcodes;
    }
    return;
}

Run it: 运行:

// output in here
$postcodes = array();

// note: input needs to be an array that looks like a `condition_combine`

getPostcodes(array($a),  $postcodes);

echo 'output start', PHP_EOL;
print_r($postcodes);
echo 'output end', PHP_EOL;
exit;

The output: 输出:

output start
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [type] => salesrule/rule_condition_address
                    [attribute] => postcode
                    [id] => lvl__2--0
                    [operator] => >
                    [value] => 00999
                    [is_value_processed] => 
                )

            [1] => Array
                (
                    [type] => salesrule/rule_condition_address
                    [attribute] => postcode
                    [id] => lvl__2--1
                    [operator] => <
                    [value] => 96200
                    [is_value_processed] => 
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [type] => salesrule/rule_condition_address
                    [attribute] => postcode
                    [operator] => >=
                    [id] => lvl__2--3
                    [value] => 97000
                    [is_value_processed] => 
                )

            [1] => Array
                (
                    [type] => salesrule/rule_condition_address
                    [attribute] => postcode
                    [id] => lvl__2--4
                    [operator] => <
                    [value] => 99500
                    [is_value_processed] => 
                )
        )
)
output end

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

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