2015-02-09 132 views
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) 
) 
+0

這將取決於在語言上。 – 2015-02-09 19:08:33

+0

@ScottHunter。謝謝,我解決了這個問題。 – 2015-02-09 19:10:44

+0

是的,但您必須編寫一些代碼才能執行此操作。嘗試一下,告訴我們如何去做,如果它不起作用,那麼我們可以提供幫助。 – 2015-02-09 19:21:44

回答

3

開始,你可以做這樣來做:

$new_array = array(); 

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

print_r($new_array); 
+0

太棒了,這個工程。謝謝!它應該是'$ v ['value'] = 2' – 2015-02-09 19:54:01

+0

小心,'$ v ['value'] = 2'會將'$ v ['value']'設爲'2' – Rudi 2015-02-09 19:59:33

+0

再次感謝您,正確的,它應該是'$ v ['value'] == 2' – 2015-02-09 20:10:22

相關問題