-1

我有一个名为“ VALUES ”的数组,其中包含多个数组。在这些数组中有一个名为“test”的字段,我只想指出包含数字 4 的数组在 test 字段中。

我当前对Values数组的输出:

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?

        }
    }
4

5 回答 5

0

不完全确定您的意思,但是如果您想遍历另一个数组中的数组,请尝试

foreach($values as $key => $innerArray) {
    if($innerArray['test'] === 4) {
         do_something();
    }
}
于 2013-09-02T14:18:42.743 回答
0

尝试以下代码:

foreach ($values as $key => &$value) 
{
   if($value['test'] != "4")
      unset($values[$key]);
}

var_dump($values);
于 2013-09-02T14:19:45.343 回答
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);
        }
    }

不?

于 2013-09-02T14:20:11.620 回答
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;
}
于 2013-09-02T14:23:53.030 回答
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);
    }
}

就是这么简单。

于 2013-09-02T14:37:16.417 回答