2016-10-03 12 views
0

に、私はこの配列を持ってご注文削除NULL値の鍵-valのペアdistorsが連想配列

[1] => Array 
    (
     [Assembly required] => Yes 
     [Max load (kg)] => 120 
     [Product weight (kg)] => 
     [Warranty (years)] => 3 
     [Height adjustable] => Yes 
     [Lumbar support] => 
     [Back tilt adjustment] => Yes 
     [Seat tilt adjustment] => Yes 
     [Chair height range (mm)] => 880 - 950 
     [Chair seat width (mm)] => 500 
     [Chair seat depth (mm)] => 480 
     [Chair back height (mm)] => 410 
     [Chair back width (mm)] => 420 
     [Seat height range (mm)] => 440 - 580 
     [AFRDI Approved] => 
     [Optional adjustable arms] => Yes 
     [image] => https://cdn.shopify.com/s/files/1/1249/7859/files/Chair.png?18135080827462508830 
    ) 

上記配列ののvar_dumpは、私が上からブランク値のペアを削除する

  [1]=> 
     array(17) { 
     ["Assembly required"]=> 
     string(3) "Yes" 
     ["Max load (kg)"]=> 
     string(3) "120" 
     ["Product weight (kg)"]=> 
     string(0) "" 
     ["Warranty (years)"]=> 
     string(1) "3" 
     ["Height adjustable"]=> 
     string(3) "Yes" 
     ["Lumbar support"]=> 
     string(0) "" 
     ["Back tilt adjustment"]=> 
     string(3) "Yes" 
     ["Seat tilt adjustment"]=> 
     string(3) "Yes" 
     ["Chair height range (mm)"]=> 
     string(9) "880 - 950" 
     ["Chair seat width (mm)"]=> 
     string(3) "500" 
     ["Chair seat depth (mm)"]=> 
     string(3) "480" 
     ["Chair back height (mm)"]=> 
     string(3) "410" 
     ["Chair back width (mm)"]=> 
     string(3) "420" 
     ["Seat height range (mm)"]=> 
     string(9) "440 - 580" 
     ["AFRDI Approved"]=> 
     string(0) "" 
     ["Optional adjustable arms"]=> 
     string(3) "Yes" 
     ["image"]=> 
     string(80) "https://cdn.shopify.com/s/files/1/1249/7859/files/Chair.png?18135080827462508830" 
     } 

です。

foreach($singlearr as $key=>$value){  
     if(is_null($value) || $value == '') 
      unset($singlearr[$key]); 
    } 

値がヌルであるが、それは順番は次の配列を歪まこの除去キーと値のペア。

[1] => Array 
    (
     [Assembly required] => Yes 
     [Max load (kg)] => 120 
     [Warranty (years)] => 3 
     [Height adjustable] => Yes 
     [Back tilt adjustment] => Yes 
     [Chair height range (mm)] => 880 - 950 
     [Chair seat width (mm)] => 500 
     [Chair seat depth (mm)] => 480 
     [Chair back height (mm)] => 410 
     [Chair back width (mm)] => 420 
     [Seat height range (mm)] => 440 - 580 
     [image] => https://cdn.shopify.com/s/files/1/1249/7859/files/Chair.png?18135080827462508830 
     [Seat tilt adjustment] => Yes 
     [Optional adjustable arms] => Yes 
    ) 

結果アレイのvar_dump形式:例えばのための

  [1]=> 
     array(14) { 
     ["Assembly required"]=> 
     string(3) "Yes" 
     ["Max load (kg)"]=> 
     string(3) "120" 
     ["Warranty (years)"]=> 
     string(1) "3" 
     ["Height adjustable"]=> 
     string(3) "Yes" 
     ["Back tilt adjustment"]=> 
     string(3) "Yes" 
     ["Chair height range (mm)"]=> 
     string(9) "880 - 950" 
     ["Chair seat width (mm)"]=> 
     string(3) "500" 
     ["Chair seat depth (mm)"]=> 
     string(3) "480" 
     ["Chair back height (mm)"]=> 
     string(3) "410" 
     ["Chair back width (mm)"]=> 
     string(3) "420" 
     ["Seat height range (mm)"]=> 
     string(9) "440 - 580" 
     ["image"]=> 
     string(80) "https://cdn.shopify.com/s/files/1/1249/7859/files/Chair.png?18135080827462508830" 
     ["Seat tilt adjustment"]=> 
     string(3) "Yes" 
     ["Optional adjustable arms"]=> 
     string(3) "Yes" 
     } 

。上記の結果配列では、[image]が上に移動しそうです。

答えて

3

あなたはそれがあなたのヌルを削除するには、これを使用し

official documentation

+0

...配列からすべての空とnull値を除外します

array_filter()

を使用することができます配列$ arr = array_filter($ arr)の値フィールド。 –

+0

@NaveenKumar @jaimin array_filter()は空を削除しましたが、チェックする必要はありませんでしたが、前の順序を歪ませます。私は 'CSV'ファイルから配列を取り出しています –