1

this is my code , and I am trying to loop through this json, so what I want to do is when isset($_GET['latest=1'])) it should show one object from json above, if latest=15 it should show 15 objects, how can I do that

    <?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" 
            }
        ]
    }
';
Festim Dehar
  • 87
  • 11

1 Answers1

1

I would recommend that you convert your data into an array and then re-encode after you have sorted your data.

The following below maybe of some help:

URL:

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

Code - Part 1:

This is a copy of your JSON data above.

$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" 
            }
        ]
    }';

Code - Part 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

Output (For value of 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"
}]

Added: Also added a break in case they ask for a value which is not defined in your array.

Note:

Shorthand ternary will only work with PHP 5.3 so I suspect your error is to do with this. PHP Shorthand ternary operator "?:" Parse error unexpected ":"

Community
  • 1
  • 1
Kitson88
  • 2,619
  • 5
  • 18
  • 35