[英]How to get Array data by key value from multidimensional array?
我在我的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()
以及其他一些方式,但是它們僅在我們具有一級子數組的情況下才有效。 基本上,我只想從此數組中獲取郵政編碼數據,而不使用多級循環。 任何幫助是極大的贊賞。
必需:從以數組形式保存的tree
提取某些標識為“郵政編碼”的leaf nodes
。
有一個問題:
1)輸入數組“ root”是一個關聯的數組(“ condition”),而不是像樹的其余部分一樣的“ conditions”數組。 這說明了為什么我使用array($ sourceData)而不是$ sourceData來開始處理。
/**
* 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.