比较2个数组之间的2个值

时间:2022-08-13 22:51:06

My code looks like:

我的代码看起来像:

foreach($client->GetSMSMessages($params)->GetReceivedMessages->ReceivedMessage as $value){

   foreach($get_customer as $customer){

      if(substr($customer['mobile'],1) == $value->MobileNumber || $customer['mobile'] == $value->MobileNumber){

      $name = $customer['name'];

     }else{
     $name = '';
     }
  }
$json['received'][] = array (
//'rec_id' => $value->SMSID,
'rec_sms' => $value->Body,
'rec_customer' => $name, 
'rec_tel' =>  $value->MobileNumber,
'rec_date' => $value->DateTime
 );
 }

What i'm trying to achieve without success is to compare the variable $customer['mobile'] to $value->MobileNumber and if there's any value matching to print the customers name ($name = $customer['name']).

我想要取得的成功是将变量$ customer ['mobile']与$ value-> MobileNumber进行比较,如果有任何值匹配则打印客户名称($ name = $ customer ['name']) 。

Any ideas ?

有任何想法吗 ?

Thanks for helping

谢谢你的帮助

1 个解决方案

#1


1  

Stop the loop when you find a match, so you don't set $name back to '' on the next iteration.

找到匹配项时停止循环,因此在下一次迭代时不要将$ name设置回''。

Actually, there's no need to set $name = '' every time. Just initialize it before the loop, and set it to the found customer name when you find a match.

实际上,没有必要每次都设置$ name =''。只需在循环之前初始化它,并在找到匹配项时将其设置为找到的客户名称。

foreach($client->GetSMSMessages($params)->GetReceivedMessages->ReceivedMessage as $val){
    $name = '';
    foreach($get_customer as $customer){
        if(substr($customer['mobile'],1) == $value->MobileNumber || $customer['mobile'] == $value->MobileNumber){
            $name = $customer['name'];
            break;
        }
    }
    $json['received'][] = array (
        //'rec_id' => $value->SMSID,
        'rec_sms' => $value->Body,
        'rec_customer' => $name, 
        'rec_tel' =>  $value->MobileNumber,
        'rec_date' => $value->DateTime
    );
}

#1


1  

Stop the loop when you find a match, so you don't set $name back to '' on the next iteration.

找到匹配项时停止循环,因此在下一次迭代时不要将$ name设置回''。

Actually, there's no need to set $name = '' every time. Just initialize it before the loop, and set it to the found customer name when you find a match.

实际上,没有必要每次都设置$ name =''。只需在循环之前初始化它,并在找到匹配项时将其设置为找到的客户名称。

foreach($client->GetSMSMessages($params)->GetReceivedMessages->ReceivedMessage as $val){
    $name = '';
    foreach($get_customer as $customer){
        if(substr($customer['mobile'],1) == $value->MobileNumber || $customer['mobile'] == $value->MobileNumber){
            $name = $customer['name'];
            break;
        }
    }
    $json['received'][] = array (
        //'rec_id' => $value->SMSID,
        'rec_sms' => $value->Body,
        'rec_customer' => $name, 
        'rec_tel' =>  $value->MobileNumber,
        'rec_date' => $value->DateTime
    );
}