Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // http://stackoverflow.com/questions/35178357/how-to-get-array-data-by-key-value-from-multidimensional-array
- // include __DIR__ .'/__bootstrap__.php';
- $a = Array(
- "type" => "salesrule/rule_condition_combine",
- 'id' => 'root',
- "attribute" => "",
- "operator" => "",
- "value" => "1",
- "is_value_processed" => "",
- "aggregator" => "any",
- "conditions" => Array
- (
- "0" => Array
- (
- "type" => "salesrule/rule_condition_combine",
- "attribute" => "",
- 'id' => 'lvl__1--0',
- "operator" => "",
- "value" => "1",
- "is_value_processed" => "",
- "aggregator" => "all",
- "conditions" => 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
- (
- "type" => "salesrule/rule_condition_combine",
- "attribute" => "",
- 'id' => 'lvl__1--1',
- "operator" => "",
- "value" => "1",
- "is_value_processed" => "",
- "aggregator" => "all",
- "conditions" => 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" => "",
- )
- )
- )
- )
- );
- echo 'test data start', PHP_EOL;
- print_r($a);
- echo 'test data end', PHP_EOL;
- // 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;
- // --------------------------------------------------------------------------
- /**
- * 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'])) {
- getPostcodes($condition['conditions'], $postcodes);
- continue; // process next entry...
- }
- if ( isset($condition['attribute'])
- && $condition['attribute'] === 'postcode') {
- $currentPostcodes[] = $condition;
- }
- }
- if (!empty($currentPostcodes)) {
- $postcodes[] = $currentPostcodes;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment