Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在json php循环中查找变量_Php_Json_Search - Fatal编程技术网

在json php循环中查找变量

在json php循环中查找变量,php,json,search,Php,Json,Search,我需要用php在json数组中查找电话号码。我正在尝试foreach,但问题是如果找不到电话号码,我会得到多条记录。还有没有其他方法可以写,所以我只得到一个回复。如果找不到手机,我需要发回,但它会为每个找不到的记录创建一个记录 我的回答是这样的 找不到 找不到 发现 找不到 找不到 我需要1个未找到的不是多个 $curl = curl_init(); $usertype='x-cw-usertype: integrator'; curl_setopt($curl, CURLOPT_SSL

我需要用php在json数组中查找电话号码。我正在尝试foreach,但问题是如果找不到电话号码,我会得到多条记录。还有没有其他方法可以写,所以我只得到一个回复。如果找不到手机,我需要发回,但它会为每个找不到的记录创建一个记录

我的回答是这样的

找不到

找不到

发现

找不到

找不到

我需要1个未找到的不是多个

$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>';

} 
}
$curl=curl_init();
$usertype='x-cw-usertype:integrator';
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,1);
curl_setopt($curl,CURLOPT_URL,“/api/3.0/company/contacts”);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_HTTPHEADER,数组(
“授权:基本”.base64_编码($username.:'。$password),
'clientId:',
$usertype,
'内容类型:应用程序/json'
));
$response=curl\u exec($curl);
curl_close($curl);
$json_contacts=json_decode($response,true);
//var_dump($json_联系人);
}
//循环阵列
foreach($json_联系人作为$item){
//如果找到电话号码
如果($item['defaultPhoneNbr']=='5551212'){
回声“发现”
; } //如果找不到电话 如果($item['defaultPhoneNbr']!='5551212'){ 回声“未找到
”; } }
创建用于存储结果的变量。比如:

foreach ( $json_contacts  as $item ) { 

// Check item and the return value of  
// reset() function is equal then it  
// will be the first iteration 
if ( $item['defaultPhoneNbr'] == '6102070035' ) { 

    // Desplay the array element 

    $result = “found”;
    break;
 } 


// Check if item and return value of  
// end() function is equal then it 
// will be last iteration 
else if  ( $item['defaultPhoneNbr'] != '6102070035' ){ 


       $result = “not found”;
} 
}
那就附和一下吧

  echo $result;

如果你不想在循环中继续,请使用“中断”。这似乎有效,不确定为什么,但我更早尝试中断,似乎是在计时我的循环。我刚刚重拨了密码,它似乎在工作。非常感谢