2017-02-12 198 views
1

这是我的代码,我试图通过这个JSON循环,所以我想要做的是当isset($_GET['latest=1']))它应该显示一个对象从JSON上面,如果最新= 15它应该显示15个对象,我该怎么做循环遍历JSON对象php

<?php 
header('Content-Type: application/json'); 
if(isset($_GET['latest'])) 
echo 
' 
    { 
     "contacts": [ 
      { 
        "id": "c200", 
        "name": "Ravi Tamada", 
        "email": "[email protected]", 
        "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": "[email protected]", 
        "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": "[email protected]", 
        "address": "xx-xx-xxxx,x - street, x - country", 
        "gender" : "male", 


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

你从哪里取数据 - 从数据库? –

回答

1

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

下以下的也许有些帮助:

网址:

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

码 - 第1部分:

这是你的JSON数据的副本之上。

$json = ' { 
     "contacts": [ 
      { 
        "id": "c200", 
        "name": "Ravi Tamada", 
        "email": "[email protected]", 
        "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": "[email protected]", 
        "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": "[email protected]", 
        "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": "[email protected]", 
    "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": "[email protected]", 
    "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 ":"

+0

我试过了,但这是我的错误 –

+0

请分享你的错误。请确保你的JSON数据被设置为变量'$ json'。我会更新我的答案来解释。 – Kitson88

+0

解析错误:语法错误,意想不到的':'在/srv/disk14/2280797/www/rido.sportsontheweb.net/webi/api.php上线25 –