检查数组中是否存在值(Laravel或Php) [英] Check if a value exits in array (Laravel or Php)
问题描述
我有这个数组:
$list_desings_ids = array('hc1wXBL7zCsdfMu','dhdsfHddfD','otheridshere');
使用die()+ var_dumo(),此数组返回我:
With a die() + var_dumo() this array return me:
array:2 [▼
0 => "hc1wXBL7zCsdfMu"
1 => "dhdsfHddfD"
2 => "otheridshere"
]
我要检查是否在$ list_desings_ids数组中退出了design_id.
I want check if a design_id exits in $list_desings_ids array.
例如:
foreach($general_list_designs as $key_design=>$design) {
#$desing->desing_id return me for example: hc1wXBL7zCsdfMu
if(array_key_exists($design->design_id, $list_desings_ids))
$final_designs[] = $design;
}
但这对我不起作用,正确的方法是什么?
But this not works to me, what is the correct way?
推荐答案
而不是array_key_exists
您只需键入in_array
即可解决您的问题
因为如果您转储此数组
instead array_key_exists
you just type in_array
this will solve your issue
because if you dump your this array
$list_desings_ids = array('hc1wXBL7zCsdfMu','dhdsfHddfD','otheridshere');
输出将是
array(
0 => hc1wXBL7zCsdfMu,
1 => dhdsfHddfD,
2 => otheridshere
)
所以您的代码array_key_exists
将不起作用,因为这里的键0,1,2
存在,所以,您要检查值,以便获取值,只需执行in_array
,它将在您提到的内容中搜索您想要的值/创建的数组
so your code array_key_exists
will not work, because here in keys 0,1,2
exists, So, you want to check values,so for values, just do this in_array
it will search for your desire value in your mentioned/created array
这篇关于检查数组中是否存在值(Laravel或Php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!