从表中的关联数组中获取值

时间:2017-07-14 10:28:50

标签: php codeigniter

Array
(
    [0] => Array
        (
            [datas] => Array
                (
                    [res] => 1
                    [rows] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [a] => 11
                                    [b] => 901
                                    [c] => 2
                                    [d] => 2
                                    [e] => A
                                    [f] => BT 
                                    [g] => arushi
                                    [h] => arushi@gmail.com
                                    [i] => 123445
                                    [j] => 12355.jpg
                                )

                        )

                )

        )

    [1] => Array
        (
            [datas] => Array
                (
                    [res] => 1
                    [rows] => stdClass Object
                        (
                            [person_name] => arushi
                        )

                )

        )

)
  

如果我得到那种关闭数组的值,我怎么能得到它的值   两个部分用不同的变量m不能理解   数组的结构..       需要在不同的不同表中使用相同的页面单独获取表中的值,以便我可以获取值

2 个答案:

答案 0 :(得分:3)

您需要执行foreach循环。唯一的问题是,如果你想让它变得动态并自己抓取数据而不告诉它要得到什么。如果不是,则意味着您必须确切地知道您需要什么以及结果的结构。但是看到有一种模式,你可以做一些检查,直到你到达行。例如($array将是包含您提供的数据的变量):

foreach ($array AS $arr) {
    // To make you function/check work faster,
    // before even entering to fetch for data
    // you can check if it has any data, else you
    // probably will end up with an error
    if (isset ($arr ['datas']) && $arr ['datas']) {
        foreach ($arr ['datas'] AS $data) {
            if (isset ($arr ['res']) && 0 < $arr ['res']) {
                // here is where it gets tricky because
                // from you example its either an array or an object
                // but the kool part about it is that you can convert an object into an array
                // or just leave it as is because the foreach will take care of it either way :)

                // first check it it is an object and convert if yes
                $row    = $arr ['rows'];

                if (!is_array ($row))
                    $row = (array)$row; // converting it to array is super easy :)

                foreach ($row AS $key=>$dat) {
                    // from here your result can either be another array or just data
                    // so you run the check once more
                    if (is_array ($dat)) {
                        foreach ($dat AS $k=>$d) {
                            echo "data for $k is $d";
                        }
                    }
                    else
                        echo "data for $key is $dat";
                }
            }
        }
    }
}

答案 1 :(得分:2)

您可以使用foreach循环遍历从0到n的键的数组。这样您就可以通过主数组来获取所有键的数据数组。要获得子项,您可以在其中使用另一个foreach来获取每个项目。但对于行它是对象,所以你必须使用 - &gt;选择密钥。见下面的示例代码。但是在您的阵列上,第二个阵列在数组中包含不同的格式。所以让它像一个普通的甲酸盐,轻松地循环它。 例如: -

foeach($parent_array as $array){

   $data=$array['datas'];
   $res = $data['res'];
   $rows=$data['rows'];

   foreach($rows as $row){
      echo $row->a;  // to get the value of key a = 11
   }
}