2015-08-24 228 views
2

我知道這個問題被問及許多次,我已經搜索了足夠多的結果,但迄今沒有取得成功。如何從二維數組元素中檢查空單個數組元素PHP

讓說,我有這種類型的數組:

1 ~~~ 
1 ### 
Array ([macro_ratio] => 1.1 [foodCategoryId] => 37) @@@ 
2 ### 
Array ([macro_ratio] => 1.2 [foodCategoryId] => 2) @@@ 
3 ### 
Array ([macro_ratio] => 1.3 [foodCategoryId] => 3) @@@ 
4 ### 
Array ([macro_ratio] => [foodCategoryId] => 4) @@@ 
5 ### 
Array ([macro_ratio] => [foodCategoryId] => 5) @@@ 
6 ### 
Array ([macro_ratio] => [foodCategoryId] => 6) @@@ 
7 ### 
Array ([macro_ratio] => [foodCategoryId] => 7) @@@ 
8 ### 
Array ([macro_ratio] => [foodCategoryId] => 8) @@@ 
9 ### 
Array ([macro_ratio] => [foodCategoryId] => 9) @@@ 
10 ### 
Array ([macro_ratio] => [foodCategoryId] => 38) @@@ 
11 ### 
Array ([macro_ratio] => [foodCategoryId] => 10) @@@ 
12 ### 
Array ([macro_ratio] => [foodCategoryId] => 11) @@@ 
13 ### 
Array ([macro_ratio] => [foodCategoryId] => 12) @@@ 
14 ### 
Array ([macro_ratio] => [foodCategoryId] => 13) @@@ 
15 ### 
Array ([macro_ratio] => [foodCategoryId] => 14) @@@ 
16 ### 
Array ([macro_ratio] => [foodCategoryId] => 15) @@@ 
17 ### 
Array ([macro_ratio] => [foodCategoryId] => 16) @@@ 
18 ### 
Array ([macro_ratio] => [foodCategoryId] => 17) @@@ 
19 ### 
Array ([macro_ratio] => [foodCategoryId] => 39) @@@ 
20 ### 
Array ([macro_ratio] => [foodCategoryId] => 18) @@@ 
21 ### 
Array ([macro_ratio] => [foodCategoryId] => 19) @@@ 
22 ### 
Array ([macro_ratio] => [foodCategoryId] => 20) @@@ 
23 ### 
Array ([macro_ratio] => [foodCategoryId] => 21) @@@ 
24 ### 
Array ([macro_ratio] => [foodCategoryId] => 22) @@@ 
25 ### 
Array ([macro_ratio] => [foodCategoryId] => 23) @@@ 
26 ### 
Array ([macro_ratio] => [foodCategoryId] => 24) @@@ 
27 ### 
Array ([macro_ratio] => [foodCategoryId] => 25) @@@ 
28 ### 
Array ([macro_ratio] => [foodCategoryId] => 40) @@@ 
29 ### 
Array ([macro_ratio] => [foodCategoryId] => 41) @@@ 
30 ### 
Array ([macro_ratio] => [foodCategoryId] => 26) @@@ 
31 ### 
Array ([macro_ratio] => [foodCategoryId] => 27) @@@ 
32 ### 
Array ([macro_ratio] => [foodCategoryId] => 28) @@@ 
33 ### 
Array ([macro_ratio] => [foodCategoryId] => 29) @@@ 
34 ### 
Array ([macro_ratio] => [foodCategoryId] => 30) @@@ 
35 ### 
Array ([macro_ratio] => [foodCategoryId] => 34) @@@ 
36 ### 
Array ([macro_ratio] => [foodCategoryId] => 35) @@@ 
37 ### 
Array ([macro_ratio] => [foodCategoryId] => 36) @@@ 
2 ~~~ 
1 ### 
Array ([macro_ratio] => 2.1 [foodCategoryId] => 37) @@@ 
2 ### 
Array ([macro_ratio] => 2.2 [foodCategoryId] => 2) @@@ 
3 ### 
Array ([macro_ratio] => 2.3 [foodCategoryId] => 3) @@@ 
4 ### 
Array ([macro_ratio] => [foodCategoryId] => 4) @@@ 
5 ### 
Array ([macro_ratio] => [foodCategoryId] => 5) @@@ 
6 ### 
Array ([macro_ratio] => [foodCategoryId] => 6) @@@ 
7 ### 
Array ([macro_ratio] => [foodCategoryId] => 7) @@@ 
8 ### 
Array ([macro_ratio] => [foodCategoryId] => 8) @@@ 
9 ### 
Array ([macro_ratio] => [foodCategoryId] => 9) @@@ 
10 ### 
Array ([macro_ratio] => [foodCategoryId] => 38) @@@ 
11 ### 
Array ([macro_ratio] => [foodCategoryId] => 10) @@@ 
12 ### 
Array ([macro_ratio] => [foodCategoryId] => 11) @@@ 
13 ### 
Array ([macro_ratio] => [foodCategoryId] => 12) @@@ 
14 ### 
Array ([macro_ratio] => [foodCategoryId] => 13) @@@ 
15 ### 
Array ([macro_ratio] => [foodCategoryId] => 14) @@@ 
16 ### 
Array ([macro_ratio] => [foodCategoryId] => 15) @@@ 
17 ### 
Array ([macro_ratio] => [foodCategoryId] => 16) @@@ 
18 ### 
Array ([macro_ratio] => [foodCategoryId] => 17) @@@ 
19 ### 
Array ([macro_ratio] => [foodCategoryId] => 39) @@@ 
20 ### 
Array ([macro_ratio] => [foodCategoryId] => 18) @@@ 
21 ### 
Array ([macro_ratio] => [foodCategoryId] => 19) @@@ 
22 ### 
Array ([macro_ratio] => [foodCategoryId] => 20) @@@ 
23 ### 
Array ([macro_ratio] => [foodCategoryId] => 21) @@@ 
24 ### 
Array ([macro_ratio] => [foodCategoryId] => 22) @@@ 
25 ### 
Array ([macro_ratio] => [foodCategoryId] => 23) @@@ 
26 ### 
Array ([macro_ratio] => [foodCategoryId] => 24) @@@ 
27 ### 
Array ([macro_ratio] => [foodCategoryId] => 25) @@@ 
28 ### 
Array ([macro_ratio] => [foodCategoryId] => 40) @@@ 
29 ### 
Array ([macro_ratio] => [foodCategoryId] => 41) @@@ 
30 ### 
Array ([macro_ratio] => [foodCategoryId] => 26) @@@ 
31 ### 
Array ([macro_ratio] => [foodCategoryId] => 27) @@@ 
32 ### 
Array ([macro_ratio] => [foodCategoryId] => 28) @@@ 
33 ### 
Array ([macro_ratio] => [foodCategoryId] => 29) @@@ 
34 ### 
Array ([macro_ratio] => [foodCategoryId] => 30) @@@ 
35 ### 
Array ([macro_ratio] => [foodCategoryId] => 34) @@@ 
36 ### 
Array ([macro_ratio] => [foodCategoryId] => 35) @@@ 
37 ### 
Array ([macro_ratio] => [foodCategoryId] => 36) @@@ 

這個數組我想省略/跳過Array ([macro_ratio] => [foodCategoryId] => 36) @@@元素進行排序的意思是如果[macro_ratio] =>空的,所以我想跳過列的是整個元素。

我工作的代碼是:

foreach ($_POST['mealData'] as $key => $value) { 
      print_r($key); 
      echo " ~~~<br>"; 

      foreach ($value as $key => $value) { 
       print_r($key); 
       echo " ###<br>"; 
       print_r($value); 
       echo " @@@<br>"; 
       if (isset($value['macro_ratio']) === true && empty($value['macro_ratio']) === true && isset($value['foodCategoryId']) === true && empty($value['foodCategoryId']) === true) { 
        print_r($value); 
       } 
      } 
     } 
+0

什麼是macro_ratio?這是一個關鍵? – aldrin27

+0

是的,這是關鍵。 'print_r($ value);'包含上面所有的數組輸出。數組([macro_ratio] => 1.1 [foodCategoryId] => 37) Array([macro_ratio] => 1.1 [foodCategoryId] => 37) Array([macro_ratio] => 1.2 [foodCategoryId] =>數組([macro_ratio] => 1.2 [foodCategoryId] => 2) Array([macro_ratio] => 1.3 [foodCategoryId] => 3) Array ([macro_ratio] => [foodCategoryId] => 4)' – LearningROR

回答

1

值返回經過濾的陣列macro_ratio和foodCategoryId都是鍵,你可以在第一個循環後訪問。 if語句將檢查是否設置了macro_ratio而不是空的。 更改您的代碼這一點,它應該工作

$data = array(// first loop to access this level 
array(// second loop to access this level, 
    'macro_ratio' => 1.2 // check if index macro_ratio is set and not empty 
    'foodCategoryId' => 2), 
array(
    'macro_ratio' => 2 
    'foodCategoryId' => 4) 
) 
foreach ($_POST['mealData'] as $k1 => $v1) { 

foreach ($value as $k2 => $v2) { 
    check if index macro_ratio is set and not empty 
    if (isset($value2['macro_ratio']) && !empty($value2['macro_ratio'])) { 
     echo $value2['foodCategoryId']; 
    }   
} 
} 
+0

代碼沒有輸出。 – LearningROR

+0

已更新的答案。 – mdamia

+0

謝謝。它運作良好。 – LearningROR

1

試試這個,如果它工作

foreach ($_POST['mealData'] as $key => $value) { 
     print_r($key); 
     echo " ~~~<br>"; 
     if(!empty($value['macro_ratio'])){ 
      foreach($value as $key2 => $value2) { 
       print_r($value2); 
      } 
     }     

    } 
+0

output is:'1 ~~~ 2 ~~~' – LearningROR

+0

是'print_r($ value2);'? 1 ~~~ 2 ~~~輸出? – aldrin27

+0

是的,它沒有輸出任何東西,只是上面的代碼。 – LearningROR

1

這是做它的另一種方式。在foreach循環之前過濾數組。 array_filter與自定義回調將做到這一點。

這將通過在陣列中檢查每個元件以確保長度的['macro_ratio']大於0。使用​​strlen的,而不是空仍將允許的0

$filteredArray = array_filter($array, function($value) { 
    return (strlen($value['macro_ratio'])); 
}); 
1

希望這個作品......這是最簡單的方法。

<?php 
$result = array(); 
foreach ($_POST['mealData'] as $key => $value){ 
    if(empty($value["macro_ratio"])) 
     continue; 
    else 
     $result[$key] = $value; 
} 
var_dump($result); 
+0

輸出是:'array(0){}' – LearningROR

+0

@LearningROR它在我的本地主機上運行良好。 http://i.imgur.com/fweNJD6.jpg – Zerquix18