【问题标题】:Loop throught json object php [closed]循环通过json对象php [关闭]
【发布时间】:2017-06-30 13:36:47
【问题描述】:

这是我的代码,我正在尝试遍历这个 json,所以我想要做的是当 isset($_GET['latest=1'])) 它应该显示上面 json 中的一个对象,如果 latest=15 它应该显示 15 个对象,怎么能我这样做

    <?php
header('Content-Type: application/json');
if(isset($_GET['latest']))
echo 
'
    {
        "contacts": [
            {
                    "id": "c200",
                    "name": "Ravi Tamada",
                    "email": "ravi@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",
                    "url": "http://149.202.196.143:8000/live/djemal/djemal/592.ts"
            },
            {
                    "id": "c201",
                    "name": "Johnny Depp",
                    "email": "johnny_depp@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",
                    "url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts" 
            },
            {
                    "id": "c202",
                    "name": "Leonardo Dicaprio",
                    "email": "leonardo_dicaprio@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",


    "url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts" 
            }
        ]
    }
';

【问题讨论】:

  • 你从哪里获取数据 - 从数据库?

标签: php json


【解决方案1】:

我建议您将数据转换为数组,然后在对数据进行排序后重新编码。

以下内容可能会有所帮助:

网址:

http://localhost/index.php?latest=2

代码 - 第 1 部分:

这是您上面的 JSON 数据的副本。

$json = ' {
        "contacts": [
            {
                    "id": "c200",
                    "name": "Ravi Tamada",
                    "email": "ravi@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",
                    "url": "http://149.202.196.143:8000/live/djemal/djemal/592.ts"
            },
            {
                    "id": "c201",
                    "name": "Johnny Depp",
                    "email": "johnny_depp@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",
                    "url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts" 
            },
            {
                    "id": "c202",
                    "name": "Leonardo Dicaprio",
                    "email": "leonardo_dicaprio@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",


    "url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts" 
            }
        ]
    }';

代码 - 第 2 部分:

$count = (isset($_GET['latest']) && filter_var($_GET['latest'], FILTER_VALIDATE_INT) && $_GET['latest'] >= 1) ? $_GET['latest'] : 1; //To ensure the GET is an INT and set a default value of 1.
$output = json_decode($json, TRUE); //Convert to array
$newArr = []; //Placeholder for new array

for($i=0; $i < $count; $i++) { //Dependent on value (1 being default) 

   if(isset($output['contacts'][$i])) { //Just incase you get an undefined index issue

       $newArr[] = $output['contacts'][$i]; 

   } else {

       break; //Break Loop if undefined
   }
}     

echo json_encode($newArr); //Finally output new array as JSON

输出(值为 2):

[{
    "id": "c200",
    "name": "Ravi Tamada",
    "email": "ravi@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "url": "http:\/\/149.202.196.143:8000\/live\/djemal\/djemal\/592.ts"
}, {
    "id": "c201",
    "name": "Johnny Depp",
    "email": "johnny_depp@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "url": "http:\/\/149.202.196.143:8000\/live\/djemal\/djemal\/592.ts"
}]

添加: 还添加了一个中断,以防他们要求您的数组中未定义的值。

注意:

速记三元仅适用于 PHP 5.3,因此我怀疑您的错误与此有关。 PHP Shorthand ternary operator "?:" Parse error unexpected ":"

【讨论】:

  • 我试过但这是我的错误
  • 请分享您的错误。请确保您的 JSON 数据被设置为变量 $json。我会更新我的答案来解释。
  • 解析错误:语法错误,第 25 行 /srv/disk14/2280797/www/rido.sportsontheweb.net/webi/api.php 中的意外 ':'
  • 非常感谢,一切正常,完美运行,如何标记此答案正确
猜你喜欢
  • 1970-01-01
  • 2020-02-16
  • 2016-10-11
  • 1970-01-01
  • 2021-10-30
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多