php searching in multidimensional array of unknown depth -


i try select data of array constructed json_decode(). in principle multiarray of unknown dimension.

first of all, want search recursively value in array. next step want other values of upper dimension. here example:

i search for: "2345"

.... $json[3][6]['journal']['headline']="news" $json[3][6]['journal']['article']=2345 .... $json[8]['journal']['headline']="weather" $json[8]['journal']['article']=2345 .... 

after want value of element headline (returning "news" , "weather")

it might element 2345 can found in different dimensions!!!

someone recursiveiteratoriterator object, have hard time iterator objects, here robust system:

<?php // traverse array , find // value while storing base key     class recurselocator     {         public  static  $saved = array();         public  static  $find;         public  static  $trigger;         public  static  function initialize($find = false)             {                 self::$find =   $find;             }          public  static  function recursive(array $array)             {                 foreach($array $key => $value) {                          if(!isset(self::$trigger) || (isset(self::$trigger) && empty(self::$trigger))) {                                 if(is_numeric($key))                                     self::$trigger  =   $key;                             }                          if(!is_array($value)) {                                 if($value == self::$find) {                                         self::$saved[self::$trigger]    =   $value;                                     }                             }                          if(is_array($value)) {                                  $value  =   self::recursive($value);                                 if(!is_numeric($key))                                     self::$trigger  =   "";                             }                           $return[$key]   =   $value;                     }                  return $return;             }     }  // class traverse array searching // specific key or keys class   recursesearch     {         public  $data;         public  $compare;          public  function find($array = '',$find,$recursive = true)             {                 $find   =   (is_array($find))? implode("|",$find):$find;                 if(is_array($array)) {                          foreach($array $key => $value) {                          if(preg_match("/$find/",$key))                             $this->compare[$key]    =   $value;                                  if($recursive == true) {                                         if(!is_array($value)) {                                                 if(preg_match("/$find/",$key)) {                                                         $this->data[$key][] =   $value;                                                     }                                                  $array[$key]    =   $value;                                             }                                         else {                                                 if(preg_match("/$find/",$key))                                                     $this->data[$key][] =   $this->find($value,$find);                                                  $array[$key]    =   $this->find($value,$find);                                             }                                     }                                 else {                                         if(preg_match("/$find/",$key))                                             $this->data[$key]   =   $value;                                     }                             }                          $this->data =   (isset($this->data))? $this->data:false;                          return $this;                     }             }     }  // function wraps recursesearch class function get_key_value($array = array(), $find = array(),$recursive = true)     {         $finder =   new recursesearch();         return $finder->find($array,$find,$recursive);     } 

usage:

    $json[3][6]['journal']['headline']  =   "news";     $json[3][6]['journal']['article']   =   2345;     $json[8]['journal']['headline']     =   "weather";     $json[8]['journal']['article']      =   2345;     $json[4][1]['journal']['headline']  =   "news";     $json[4][1]['journal']['article']   =   22245;     $json[5]['journal']['headline']     =   "weather";     $json[5]['journal']['article']      =   233345;      // set search criteria     recurselocator::initialize(2345);     // traverse array looking value     $arr    =   recurselocator::recursive($json);     // if found, stored here     $iso    =   recurselocator::$saved;      /* $iso looks like:     array     (         [3] => 2345         [8] => 2345     )     */      // loop through $iso array     foreach($iso $key => $value) {             // save new array search results             $new[]  =   get_key_value($json[$key],array("headline","article"),true);         }      /* $new looks like: array (     [0] => recursesearch object         (             [data] => array                 (                     [headline] => array                         (                             [0] => news                         )                      [article] => array                         (                             [0] => 2345                         )                 )              [compare] => array                 (                     [headline] => news                     [article] => 2345                 )         )      [1] => recursesearch object         (             [data] => array                 (                     [headline] => array                         (                             [0] => weather                         )                      [article] => array                         (                             [0] => 2345                         )                 )              [compare] => array                 (                     [headline] => weather                     [article] => 2345                 )         ) )     */ ?> 

just side note, above class stores multiple found in [data], , stores them in [compare], [compare] overwrite if multiple same-keys found in 1 array [data] keep adding values.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -