我想在 PHP 中执行 IF,从 API 获取数据并将这些数据与 PHP 变量中的数据进行比较,如果数据相同,则对数据详细信息执行 foreach,而不是对来自 API 的所有数据进行 foreach。
JSON:
{"10":{"name":"Cassius Mcbride"},"20":{"name":"Lena Walton"}}';
PHP:
<?php
$curl_handle = curl_init();
$api_url = 'urlToAPI';
curl_setopt($curl_handle, CURLOPT_URL, $api_url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$curl_data = curl_exec($curl_handle);
curl_close($curl_handle);
$response_data = json_decode($curl_data, true);
$user_data = $response_data;
$test = 10;
if($user_data->$data == $test) //ye I know it's don't have right working but I don't know how otherwise show what I mean
{
foreach ($user_data as $data=>$data_detals )
{
echo "<br />";
echo "id: ".$data;
echo "<br />";
echo "name: ".$data_detals['name'];
echo "<br />";
echo "<br />";
}
}
?>
我做了这样的事情:
$test = 10;
foreach ($user_data as $data=>$data_detals )
{
if($data == $test)
{
echo "<br />";
echo "id: ".$data;
echo "<br />";
echo "name: ".$data_detals['name'];
echo "<br />";
echo "<br />";
}
}
它当然有效,但我想如何以其他方式做到这一点,即不要对所有数据使用 foreach,而仅对详细信息使用,而 $data (id) 使用其他方式检查以使用更少的功率这。我想跳过其他寄存器的 foreach 。这可能吗?