dongzhan7253 2017-07-14 10:28
浏览 51
已采纳

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

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
                        )

                )

        )

)

if i get the value in that kind off array how can i get the value of both partially with different variable m not able to understand the structure of the array.. need to get the value in table seperatly in different different table with same page how i can get the values

  • 写回答

2条回答 默认 最新

  • dpg76975 2017-07-14 14:15
    关注

    You would need to do a foreach loop. The only problem is that if you want to make it dynamic and grab the data by itself without you telling it what to get. If not it means you would have to know exactly what you need and the structure of your results. But seeing that there is a pattern you can do some checks until you reach rows. For example ($array would be the variable that has the data you provided):

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?