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

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏