我的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] =>
)
)
)
)
)
这里,数组的长度可以很大。 现在我想从我的数组中只获取邮政编码值,如
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
)
)
)
我有多个循环和条件来执行此操作。
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;
这有效,但我认为这不是一种正确的方法。我还尝试使用array_map()
和array_column()
以及其他一些方法,但只有当我们有一个级别的子数组时它们才有效。
基本上我只想从这个数组中获取邮政编码数据而不使用多级循环。
非常感谢任何帮助。
答案 0 :(得分:1)
必填:提取标识为&#39;邮政编码的某些leaf nodes
&#39;来自作为数组持有的tree
。
有一个问题:
1)输入数组&#39; root&#39;是一个关联数组(&#39;条件&#39;)而不是一系列条件&#39;像树的其余部分一样。这解释了为什么我使用数组($ sourceData)而不是$ sourceData来开始处理。
Demonstration at evel.in Source code at Pastebin.com
/**
* 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;
}
// 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;
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