0

我可以删除前三个元素以及value不等于 2 的所有剩余元素吗?语言是php。

print_r($array)

输出

Array ( 
    [0] => Array ( [name] => aa [value] => 0 )        
    [1] => Array ( [name] => db [value] => 1 ) 
    [2] => Array ( [name] => bc [value] => 2 )
    [3] => Array ( [name] => ba [value] => 3 ) 
    [4] => Array ( [name] => ba [value] => 0 ) 
    [5] => Array ( [name] => bc [value] => 1 )    
    [6] => Array ( [name] => aa [value] => 2 ) 
    [7] => Array ( [name] => cb [value] => 3 )
    [8] => Array ( [name] => ca [value] => 0 ) 
    [9] => Array ( [name] => dc [value] => 1 ) 
    [10] => Array ( [name] => db [value] => 2 )
    [11] => Array ( [name] => aa [value] => 3 )
    [12] => Array ( [name] => dd [value] => 0 )
    [13] => Array ( [name] => db [value] => 1 )
    [14] => Array ( [name] => bb [value] => 2 )
    [15] => Array ( [name] => ac [value] => 3 )
) 

我还需要重新索引,使其从 0 开始

Array ( 
    [0] => Array ( [name] => aa [value] => 2 ) 
    [1] => Array ( [name] => db [value] => 2 )
    [2] => Array ( [name] => bb [value] => 2 )
) 
4

1 回答 1

3

你可以这样做:

$new_array = array();

foreach ($array as $k => $v) {
    if ($k > 2 && $v['value'] == 2) {
        $new_array[] = $v;
    }
}

print_r($new_array);
于 2015-02-09T19:29:46.653 回答