doushang9172 2014-05-21 05:21
浏览 33
已采纳

PHP计数返回的JSON长度错误

This is the JSON response that I'm getting from database. I want to print these data. For now, there's only 2 entries in my table. So the length of JSON should be 2. As data increases, count has to get increase. SO for showing output, I use a for loop. And I used count() for limiting the iteration of loop only once through the JSON.

MY JSON:

{
    "log": [
        {
            "action": "qq",
            "id": "1",
            "Time": "2014-05-19T15:40:06+05:30",
            "user": {
                "firstName": "dani",
                "type": {
                    "zzs": "1",
                    "typename": "lolo",
                    "id": "1",
                    "zzt": "1",
                    "zzu": "1",
                    "zzv": "1",
                    "zzw": "1",
                    "zzx": "1"
                },
                "id": "1",
                "lastName": "fed",
                "password": "lmfao",
                "userName": "fyi"
            },
            "userIpAddress": "101.15.23.45"
        },
        {
            "action": "vv",
            "id": "2",
            "Time": "2014-05-20T10:16:33+05:30",
            "user": {
                "firstName": "dani",
                "type": {
                    "zzs": "1",
                    "typename": "lolo",
                    "id": "1",
                    "zzt": "1",
                    "zzu": "1",
                    "zzv": "1",
                    "zzw": "1",
                    "zzx": "1"
                },
                "id": "1",
                "lastName": "web",
                "password": "rolf",
                "userName": "asap"
            },
            "userIpAddress": "192.168.0.181"
        }
]
}

MY PHP

$out = json_decode($json_data, true);
$x= count($out);
echo $x;

The value that I get is 1 instead of 2. And as you can see I have an associative array. I was trying to print those datas.

for($i=0; $i<$x; $i++)
{
echo $out['action'];
echo $out['user'][$i]['firstName'] ;
echo $out['user']['type'][$i]['typename'] ;
}

I don't get output. HELP???

  • 写回答

2条回答 默认 最新

  • dsvd407787736 2014-05-21 05:25
    关注

    you have 2 element inside log key in your array. try this:

    $x= count($out['log']);
    

    this will count 2

    demo

    for your loop you should try like this:

    $out = json_decode($json_data, true);
    $x= count($out['log']);
    $out = $out['log'];
    for($i=0; $i<$x; $i++)
    {
    echo $out[$i]['action'];
    echo $out[$i]['user']['firstName'] ;
    echo $out[$i]['user']['type']['typename'] ;
    }
    

    complete demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用