【发布时间】:2019-11-30 19:57:02
【问题描述】:
我需要用 php 在 json 数组中查找电话号码。我正在尝试 foreach,但问题是如果找不到电话号码,我会得到多条记录。无论如何要写 else 所以我只得到 1 个响应。如果找不到电话,我需要回帖,但它会为每个未找到的记录创建一个记录。
我的回复是这样的
找不到
找不到
找到
找不到
没有找到
我需要 1 Not found not multiple。
$curl = curl_init();
$usertype='x-cw-usertype: integrator';
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_URL, "/apis/3.0/company/contacts");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: basic ' . base64_encode($username . ':' . $password),
'clientId: ',
$usertype,
'Content-type: application/json'
));
$response = curl_exec($curl);
curl_close($curl);
$json_contacts = json_decode($response, true);
//var_dump($json_contacts);
}
// Loop through Array
foreach ( $json_contacts as $item ) {
// If a phone number is found
if ( $item['defaultPhoneNbr'] == '5551212' ) {
echo 'found <br>';
}
// If phone is not found
else if ( $item['defaultPhoneNbr'] != '5551212' ){
echo 'Not found <br>';
}
}
【问题讨论】: