rfv123

Q35178357/how-to-get-array-data-by-key-value

Feb 3rd, 2016
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. <?php // http://stackoverflow.com/questions/35178357/how-to-get-array-data-by-key-value-from-multidimensional-array
  2. // include __DIR__ .'/__bootstrap__.php';
  3.  
  4. $a = Array(
  5.     "type" => "salesrule/rule_condition_combine",
  6.     'id' => 'root',
  7.     "attribute" => "",
  8.     "operator" => "",
  9.     "value" => "1",
  10.     "is_value_processed" => "",
  11.     "aggregator" => "any",
  12.     "conditions" => Array
  13.         (
  14.             "0" => Array
  15.                 (
  16.                     "type" => "salesrule/rule_condition_combine",
  17.                     "attribute" => "",
  18.                     'id' => 'lvl__1--0',
  19.                     "operator" => "",
  20.                     "value" => "1",
  21.                     "is_value_processed" => "",
  22.                     "aggregator" => "all",
  23.                     "conditions" => Array
  24.                         (
  25.                             "0" => Array
  26.                                 (
  27.                                     "type" => "salesrule/rule_condition_address",
  28.                                     "attribute" => "postcode",
  29.                                     'id' => 'lvl__2--0',
  30.                                     "operator" => ">",
  31.                                     "value" => "00999",
  32.                                     "is_value_processed" => "",
  33.                                 ),
  34.  
  35.                             "1" => Array
  36.                                 (
  37.                                     "type" => "salesrule/rule_condition_address",
  38.                                     "attribute" => "postcode",
  39.                                     'id' => 'lvl__2--1',
  40.                                     "operator" => "<",
  41.                                     "value" => "96200",
  42.                                     "is_value_processed" => "",
  43.                                 ),
  44.                         )
  45.                 ),
  46.             "1" => Array
  47.                 (
  48.                     "type" => "salesrule/rule_condition_combine",
  49.                     "attribute" => "",
  50.                      'id' => 'lvl__1--1',
  51.                     "operator" => "",
  52.                     "value" => "1",
  53.                     "is_value_processed" => "",
  54.                     "aggregator" => "all",
  55.                     "conditions" => Array
  56.                         (
  57.                             "0" => Array
  58.                                 (
  59.                                     "type" => "salesrule/rule_condition_address",
  60.                                     "attribute" => "postcode",
  61.                                     "operator" => ">=",
  62.                                     'id' => 'lvl__2--3',
  63.                                     "value" => "97000",
  64.                                     "is_value_processed" => "",
  65.                                 ),
  66.  
  67.                             "1" => Array
  68.                                 (
  69.                                     "type" => "salesrule/rule_condition_address",
  70.                                     "attribute" => "postcode",
  71.                                     'id' => 'lvl__2--4',
  72.                                     "operator" => "<",
  73.                                     "value" => "99500",
  74.                                     "is_value_processed" => "",
  75.                                 )
  76.                         )
  77.                 )
  78.         )
  79. );
  80.  
  81. echo 'test data start', PHP_EOL;
  82. print_r($a);
  83. echo 'test data end', PHP_EOL;
  84.  
  85. // output in here
  86. $postcodes = array();
  87.  
  88. // note: input needs to be an array that looks like a `condition_combine`
  89.  
  90. getPostcodes(array($a),  $postcodes);
  91.  
  92. echo 'output start', PHP_EOL;
  93. print_r($postcodes);
  94. echo 'output end', PHP_EOL;
  95. exit;
  96. // --------------------------------------------------------------------------
  97.  
  98. /**
  99.  * function recurse the array looking for post codes...
  100.  *
  101.  * @param array $conditions
  102.  * @param array $postcodes by reference
  103.  * @return void
  104.  */
  105. function getPostcodes($conditions, &$postcodes) {
  106.  
  107.     $currentPostcodes = array();
  108.  
  109.     foreach ($conditions as $condition) {
  110.  
  111.         if (!empty($condition['conditions'])) {
  112.            getPostcodes($condition['conditions'], $postcodes);
  113.            continue; // process next entry...
  114.         }
  115.  
  116.         if (   isset($condition['attribute'])
  117.              && $condition['attribute']  === 'postcode') {
  118.             $currentPostcodes[] = $condition;
  119.         }
  120.     }
  121.  
  122.     if (!empty($currentPostcodes)) {
  123.         $postcodes[] = $currentPostcodes;
  124.     }
  125.     return;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment