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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测