0

I have an array

$array1 = Array (
       [a1] => Array  (
               [a_name] => aaaaa
               [a_value] => aaa
             )

       [b1] => Array (
               [b_name] => bbbbb
               [b_value] => bbb
           )
       [c1] => Array (
               [c_name] => ccccc
               [c_value] => ccc
           )

     )

Now I want to extract the value of $array1[b1][b_name]. But the thing here is the keys (b1 and b_name) will be generated dynamically. The situation here is I have a multidimensional array and the keys of which i want the value. So how do i get the value.

eg.

$array1[b1][b_name] 

should return

bbbbb

and

$array1[c1] 

should return

array([c_name]=>ccccc
       [c_value]=>ccc
   ) 

and so on...

EDIT

Lets keep it this way, The second array is

$array2 = Array (
         [b1] => Array (
               [b_name]=> zzzzz
             )
      )

Now Intersecting $array1 and $array2, I want the value of the $array1 ie. bbbbb

0

2 Answers 2

4

Try this

<?php
   //you will have $firstkey and $secondkey as index values of $array1
   if (isset($firstkey) && array_key_exists($firstkey, $array1)) {
       if (isset($secondkey) && array_key_exists($secondkey, $array1[$firstkey])) {
           print_r($array1[$firstkey][$secondkey]);
       }
       else {
           print_r($array1[$firstkey]);
           echo "$secondkey does not exist";           
       }
   }
   else {
       echo "$firstkey does not exist";
   }
?>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a foreach loop to get at both the keys and the values. I am not certain though that it will help you if you only want specific ones and you don't know how to identify them:

foreach ($array1 as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}

// From your example of the array inside a1:
// the $key would be: a_name
// the $value would be: aaaaa

Comments

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.