3 回答
TA贡献1789条经验 获得超8个赞
您可以在循环中添加条件以检查想要的行,当找到时,可以将其存储在这样的变量中:
<?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
TA贡献2041条经验 获得超4个赞
您可以使用以下内容:
$id = 2;
getTitle($data, $id);
function getTitle($data = null, $id = null)
{
foreach ($data as $datavalue) {
if ($datavalue->id == $id) {
echo $datavalue->title;
echo "<br/>";
}
}
}
- 3 回答
- 0 关注
- 214 浏览
添加回答
举报