嗨,我正在尝试从所拥有的JSON格式中获取特定数据。 我在练习中使用的是预制的json。
JSON格式是这样的:
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio
reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et
cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt
rem eveniet architecto"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor
beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil
molestiae
ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
},
{
"userId": 1,
"id": 3,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
"body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut
ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius
odio et labore et velit aut"
}
]
以此类推...
所以我要做的是从每个对象中获取标题数组。
这是我的代码:
<?php
$url = "https://jsonplaceholder.typicode.com/posts" ;
// Initiate curl
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch) ;
curl_close($ch) ;
$data = json_decode($result);
foreach ($data as $datavalue) {
echo $datavalue->title;
echo "<br/>" ;
}
现在我要显示的是特定对象的标题,比如说用户ID = 2。
我该如何说服。 请帮助我的查询。任何帮助都非常感谢。
答案 0 :(得分:1)
您可以在循环中添加条件以检查想要的行,当找到时,可以将其存储在这样的变量中:
<?php
$data = json_decode($result);
$data = json_decode($result);
$userId = 1;
$result = null;
foreach ($data as $datavalue) {
echo $datavalue->title;
if ($datavalue->userId == $userId) {
$result = $datavalue;
break;
}
echo "<br/>" ;
}
var_dump($result); // if you want to display the title you can simply do $result->title but remember to check if $result is not null
答案 1 :(得分:0)
您可以使用以下内容:
$id = 2;
getTitle($data, $id);
function getTitle($data = null, $id = null)
{
foreach ($data as $datavalue) {
if ($datavalue->id == $id) {
echo $datavalue->title;
echo "<br/>";
}
}
}
答案 2 :(得分:0)
您可以使用php array_filter()
函数。
//Your test data
$data = "[{\"userId\": 2,\"id\": 1,\"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\"body\": \"quia et suscipitsuscipit recusandae consequuntur expedita et cumreprehenderit molestiae ut ut quas totamnostrum rerum est autem sunt rem eveniet architecto\"},{\"userId\": 1,\"id\": 2,\"title\": \"qui est esse\",\"body\": \"est rerum tempore vitaesequi sint nihil reprehenderit dolor beatae ea dolores nequefugiat blanditiis voluptate porro vel nihil molestiae ut reiciendisqui aperiam non debitis possimus qui neque nisi nulla\"},{\"userId\": 1,\"id\": 3,\"title\": \"ea molestias quasi exercitationem repellat qui ipsa sit aut\",\"body\": \"et iusto sed quo iurevoluptatem occaecati omnis eligendi aut advoluptatemdoloribus vel accusantium quis pariaturmolestiae porro eius odio et labore et velit aut\"}]";
//Decode the data to a PHP array rather than object
$data = json_decode( $data, true );
const searchKey = "userId";
const searchTerm = 2;
//Filter that array
$data = array_filter( $data, function( $value ){
//If the value in the searched key matches the value, return true
if( $value[ searchKey ] === searchTerm ){
return true;
} else {
return false;
}
});
您会看到我正在使用两个const
和searchKey
两个变量,您可以将它们更改为您希望在PHP {{1}中搜索的值}功能。
以下是有关PHP searchTerm
函数的一些文档,您可能会觉得有用