[英]check associative array contains value
Array (
[0] => Array (
[questionID] => 47
[surveyID] => 51
[userID] => 31
[question_Title] => Choose Any One?
[question_Type] => Dropdown
[response] => 1.Android 2.Windows 3.Blackberry
[required] => 0
[add_time] => 0
)
[1] => Array (
[questionID] => 48
[surveyID] => 51
[userID] => 31
[question_Title] => Is it?
[question_Type] => Bigbox
[response] => Yes No
[required] => 1
[add_time] => 0
)
[2] => Array (
[questionID] => 129
[surveyID] => 51
[userID] => 31
[question_Title] => sELECT
[question_Type] => Single
[response] => DFG HBK GHCK HK
[required] => 0
[add_time] => 0
)
)
this is my multidimensional 这是我的多维
Now i want to initially check array contains [required] => 1 or [required] => 0 现在我想最初检查数组是否包含[required] => 1或[required] => 0
i don't want to traversing array 我不想遍历数组
Let's say your array name is $myarray
假设您的阵列名称为
$myarray
So you can do something like this - 所以你可以做这样的事情-
foreach($myarray as $arrelement)
{
if(isset($arrelement[required]) && $arrelement[required] == 1) //choose 1 or 0 in code as per your need
{
//traverse your array and do needful
}
}
if($yourArr[0]['required']==0) //Just check for first element in your array
echo "Zero";
else
echo "One";
foreach($mainarray as $array ){ //check for each
if($array['required']==1)
//doSome Task
if($array['required']==0)
This is probably the most hackish solution I'll come up with all this morning, but if you are shooting for a quick one-liner, this should do: 这可能是今早我将要提出的最骇人听闻的解决方案,但是如果您要拍摄一线快车,则应该这样做:
if(in_array(1, array_column($myArray, 'required')))
echo 'required';
Edit: This assumes you've got PHP 5.5+ at hand 编辑:这假设您手边有PHP 5.5以上
Update: After a closer look at the documentation of in_array()
, this should be possible: 更新:仔细查看
in_array()
的文档后,应该可以这样:
if(in_array(array('required' => 1), $myArray))
echo 'required';
As an alternative, my way independent of the array structure (assume $x holds your data structure): 另一种选择是,我的方式独立于数组结构(假设$ x保存您的数据结构):
if (preg_match('/"required":[01]/',json_encode($x))) {
// The array structure contains "required" => 0 or "required" => 1
} else {
// Negative
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.