从动态生成键的多维数组中提取值 [英] Extracting values from multidimensional array where keys are dynamically generated

查看:61
本文介绍了从动态生成键的多维数组中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组

$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
           )

     )

现在,我要提取$ array1 [b1] [b_name]的值.但是这里的事情是密钥(b1和b_name)将动态生成.这里的情况是我有一个多维数组,并且我想要键的值.那么我该如何获得价值.

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.

例如

$array1[b1][b_name] 

应该返回

bbbbb

$array1[c1] 

应该返回

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

以此类推...

编辑

让我们保持这种方式,第二个数组是

Lets keep it this way, The second array is

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

现在与$ array1和$ array2相交,我想要$ array1的值,即.bbbbb

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

推荐答案

尝试一下

<?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";
   }
?>

这篇关于从动态生成键的多维数组中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆