2017-10-18 154 views
-1

我有以下數組。如何刪除[1]中沒有值的那些值?所以如果沒有「x」,它將被刪除。PHP從數組中刪除

前:

Array 
(
[0] => Array 
    (
     [0] => 1 
     [1] => x 
     [2] => name1 
     [3] => company1 
     [4] => 709 
    ) 

[1] => Array 
    (
     [0] => 2 
     [1] => x 
     [2] => name2 
     [3] => company2 
     [4] => 500 
    ) 
. 
. 
. 
[978] => Array 
    (
     [0] => 946 
     [1] => 
     [2] => name946 
     [3] => company946 
     [4] => 0 
    ) 

[979] => Array 
    (
     [0] => 946 
     [1] => x 
     [2] => name946 
     [3] => company946 
     [4] => 0 
    ) 

[980] => Array 
    (
     [0] => 946 
     [1] => 
     [2] => name946 
     [3] => company946 
     [4] => 0 
    ) 
) 

後:

Array 
(
[0] => Array 
    (
     [0] => 1 
     [1] => x 
     [2] => name1 
     [3] => company1 
     [4] => 709 
    ) 

[1] => Array 
    (
     [0] => 2 
     [1] => x 
     [2] => name2 
     [3] => company2 
     [4] => 500 
    ) 
. 
. 
. 
[979] => Array 
    (
     [0] => 946 
     [1] => x 
     [2] => name946 
     [3] => company946 
     [4] => 0 
    ) 


) 
+0

你嘗試過這麼遠嗎? – ArtOsi

+0

只需遍歷子數組,然後刪除沒有x的子數組x' – Tarun

+1

'$ myArray = array_filter($ myArray,function($ element){return!empty($ element [1]);});' –

回答

-1

你的桌子上就循環:

foreach($lines AS $k => $row) { 
    if($row[1] !== 'x') { 
     unset($lines[$k]); 
    } 
}