循环通过json对象php
这是我的代码,我正在尝试循环使用这个json,所以我想做的是当循环通过json对象php,php,json,Php,Json,这是我的代码,我正在尝试循环使用这个json,所以我想做的是当isset($\u GET['latest=1'])它应该显示上面json中的一个对象,如果latest=15它应该显示15个对象,我该怎么做 <?php header('Content-Type: application/json'); if(isset($_GET['latest'])) echo ' { "contacts": [ {
isset($\u 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"
}
]
}
';
我建议您将数据转换为数组,然后在对数据排序后重新编码
以下内容可能会有所帮助:
网址:
http://localhost/index.php?latest=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
[{
"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"
}]
代码-第1部分:
http://localhost/index.php?latest=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
[{
"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"
}]
这是上面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部分:
http://localhost/index.php?latest=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
[{
"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"
}]
增加:
还添加了一个中断,以防他们请求数组中未定义的值
注:
速记三元组只适用于PHP5.3,因此我怀疑您的错误与此有关 你们在哪里从数据库获取数据?我试过了,但这是我的错误。请分享你们的错误。请确保您的JSON数据被设置为变量$JSON
。我会更新我的答案来解释。解析错误:语法错误,意外“:”在/srv/disk14/2280797/www/rido.sportsontheweb.net/webi/api.php第25行非常感谢,一切正常,工作正常,如何标记此注释正确