我试图在php(laravel框架)中使用foreach
循环匹配两个字符串。
$c_act = "ABC123";
foreach ($codes as $cd) {
$ac = $cd->Code; //ac holds our Activity code
if ($ac==$c_act) {
$act_id = $cd->ID;
}
}
此处$codes
通过ID
和Code
保存我的值数据。
[{"ID":"7","Code":"ABC-1"},{"ID":"8","Code":"ABC-OBT"},{"ID":"12","Code":"ABCD"}]
如果第一个变量不匹配,那么我需要回到foreach循环并从我拥有的下一个值开始。我试过了,但无法完成它。
如何使用php做到这一点?
提前致谢
答案 0 :(得分:2)
尝试使用此代码:
$act_id = null;
$c_act = "ABC123";
foreach ($codes as $cd) {
$ac = $cd->Code; //ac holds our Activity code
if ($ac==$c_act) {
$act_id = $cd->ID;
break;
}
}
if($act_id === null){
// there was no match
}else{
// there was a match, and the ID is stored in $act_id
}