2017-02-12 12 views
1

これは私のコードですが、私はこのjsonをループしようとしていますので、何をしたいのですか?isset($_GET['latest=1']))最新の場合は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

は、私はあなたがあなたのデータをソートした後で配列して、再エンコードにデータを変換することを推薦することをどのように行うことができます。

いくつかの助けを多分下に次:

URL:

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 on line 25 –

関連する問題