我有一个名为“ VALUES ”的数组,其中包含多个数组。在这些数组中有一个名为“test”的字段,我只想指出在测试字段中包含数字4的数组。
值数组的当前输出:
Array (
[0] => Array ( [entry_id] => 41149 [o_number] => 000001 [test1] => 000001 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[1] => Array ( [entry_id] => 41142 [o_number] => 000202[test1] => 000202 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[2] => Array ( [entry_id] => 41103 [o_number] => 000003 [test1] => 000003 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[3] => Array ( [entry_id] => 41101 [o_number] => 000044 [test1] => 000044 [test2] => 1234 [lev] => Ja [fak] => Manuel/brev [beta] => 10 [test] => 2 )
[4] => Array ( [entry_id] => 41100 [o_number] => 000542 [test1] => 000542 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[5] => Array ( [entry_id] => 41088 [o_number] => 001231 [test1] => 001231 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 3 ))
期望的输出:
Array (
[0] => Array ( [entry_id] => 41149 [o_number] => 000001 [test1] => 000001 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[1] => Array ( [entry_id] => 41142 [o_number] => 000202[test1] => 000202 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[2] => Array ( [entry_id] => 41103 [o_number] => 000003 [test1] => 000003 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[3] => Array ( [entry_id] => 41100 [o_number] => 000542 [test1] => 000542 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 ))
我尝试过foreach但是没有用
foreach ($values as $key)
{
if($key === 4)
{
//This will only show
print_r($key);
//delete array?
}
}
答案 0 :(得分:0)
不完全确定你的意思,但如果你想迭代另一个数组中的数组,请尝试
foreach($values as $key => $innerArray) {
if($innerArray['test'] === 4) {
do_something();
}
}
答案 1 :(得分:0)
请尝试以下代码:
foreach ($values as $key => &$value)
{
if($value['test'] != "4")
unset($values[$key]);
}
var_dump($values);
答案 2 :(得分:0)
我想,你应该使用
//simplified:
$values = array( [0]=>array(['test']=>4))
foreach ($values as $key=>&$sub_values)
{
if($sub_values['test'] === 4)
{
//This will only show
print_r($sub_values);
}else{
//deleting subarray
unset($sub_values);
}
}
没有
答案 3 :(得分:0)
foreach ($values as $key => $value)
{
if(checkArray())
{
continue;
}
// do other things
}
function checkArray($arr)
{
foreach ($arr as $key => $value)
{
if ($key === 'test' && $value === 4) {
return true;
}
}
return false;
}
答案 4 :(得分:0)
由于您使用的是多维数组,因此检查任何数组是否包含该键并不容易,而不会遍历它。要进行遍历,您可以使用foreach
foreach($values as $array) {
//Now here you can check if the there is `test` index available or is of value `4`
if(isset($array['test'])) && $array['test'] == '4') {
//Only then output it
var_dump($array);
}
}
就这么简单。