2

I would like to search key in multidimensional array and i would like to get corrosponding value associated with that key. For e.g. I would like to extract following texts from below array :

SENT AT 12.08ms

And the text

sample id 41962

following is an array print_r() output :

 Array
 (
      [0] => Array
                 (
                     [VERSION] => Array
                         (
                             [0] => Array
                                 (
                                     [group] => 
                                     [param] => Array
                                         (
                                         )

                                     [value] => Array
                                         (
                                             [0] => Array
                                                 (
                                                     [0] => 3.0
                                                 )

                                         )

                                 )

                         )

                     [SAMPLE] => Array
                         (
                             [0] => Array
                                 (
                                     [group] => 
                                     [param] => Array
                                         (
                                         )

                                     [value] => Array
                                         (
                                             [0] => Array
                                                 (
                                                     [0] => sample id 41962
                                                 )

                                         )

                                 )

                         )

                     [TSAM] => Array
                         (
                             [0] => Array
                                 (
                                     [group] => 
                                     [param] => Array
                                         (
                                         )

                                     [value] => Array
                                         (
                                             [0] => Array
                                                 (
                                                     [0] => sample group 141
                                                 )

                                             [1] => Array
                                                 (
                                                     [0] => ¯
                                                 )

                                             [2] => Array
                                                 (
                                                     [0] => sample batch 81
                                                 )

                                             [3] => Array
                                                 (
                                                     [0] => 
                                                 )

                                             [4] => Array
                                                 (
                                                     [0] => 
                                                 )

                                         )

                                 )

                         )

                     [STATUS] => Array
                         (
                             [0] => Array
                                 (
                                     [group] => 
                                     [param] => Array
                                         (
                                             [TYPE] => Array
                                                 (
                                                     [0] => CART
                                                 )

                                         )

                                     [value] => Array
                                         (
                                             [0] => Array
                                                 (
                                                     [0] => SENT AT 12.08ms
                                                 )

                                         )

                                 )

                         )

                 )
 )           

Can somebody provide me optimized code for above problem. The multidimensional array contains more than 5000 to 10000 arrays.

1
  • I know it's an array of arrays, but is every individual sub_array structured the same? Commented Dec 17, 2010 at 2:09

2 Answers 2

4

Please, see if my function works for you:

function get_value_by_key($array,$key)
{
 foreach($array as $k=>$each)
 {
  if($k==$key)
  {
   return $each;
  }

  if(is_array($each))
  {
   if($return = get_value_by_key($each,$key))
   {
    return $return;
   }
  }

 }

}

Use:

$array = array('array1'=>array('array2'=>array('find_some_key'=>'some_value')));
echo get_value_by_key($array,'find_some_key'); // outputs: some_value
Sign up to request clarification or add additional context in comments.

2 Comments

This function will retrieve ok value for specified key. The problem is the values that he needs have numeric keys like 0 (or maybe other numeric values), and you can't find them like this.
Thank you for reply.. i strongly appreciate that! It doesn't solve the issue. I'm looking for more generalized and highly optimized solution. Have implemented your and other similar solutions before but they are very specific and not highly optimized for traversing 10k to 15 k array elements.. thank you again.
3

If all the array keys have the same structure the following code should work:

foreach($array as $item){
    $sentat = $item['STATUS'][0]['value'][0][0];
    $sample = $item['SAMPLE'][0]['value'][0][0];
}

More detailed information would help us to provide you more tips :)

1 Comment

Thank you for reply.. i strongly appreciate that! it doesn't solve the issue. I'm looking for more generalized solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.