我有一个这种格式的数组:
Array ( [0] => Array ( [NUMBER] => 1 [AMOUNT] => 5 [RATE] => 1 ) [1] => Array ( [NUMBER] => 2 [AMOUNT] => 10 [RATE] => 1 ) [2] => Array ( [NUMBER] => 3 [AMOUNT] => 15 [RATE] => 1 ) )
我可以通过数组循环并找到所需的结果,但我更愿意寻找builtin
提供的Laravel
函数或函数。
如果给出两个输入,是否有有效且小的方法来搜索此数组: NUMBER = 3 和 AMOUNT = 5 然后它返回true /虚假或特定的价值?
答案 0 :(得分:1)
echo count(
array_filter(
$your_array,
function ($e) {
return $e['NUMBER'] == 3 && $e['AMOUNT'] == 5;
}
)
);
应该做的伎俩
测试:
$fooArray = array(
array(
'NUMBER' => 1,
'AMOUNT' => 5
),
array(
'NUMBER' => 2,
'AMOUNT' => 10,
),
array(
'NUMBER' => 3,
'AMOUNT' => 15
)
);
$barArray = array(
array(
'NUMBER' => 1,
'AMOUNT' => 5
),
array(
'NUMBER' => 2,
'AMOUNT' => 10,
),
array(
'NUMBER' => 3,
'AMOUNT' => 5
)
);
echo count(
array_filter(
$fooArray,
function ($e) {
return $e['NUMBER'] == 3 && $e['AMOUNT'] == 5;
}
)
);
//will echo 0
echo count(
array_filter(
$barArray,
function ($e) {
return $e['NUMBER'] == 3 && $e['AMOUNT'] == 5;
}
)
);
//will echo 1
如果您需要true / false逻辑,只需添加(true == )
或(false == )
测试而不是回显它