如何从2D数组元素PHP中检查空的单个数组元素

时间:2022-06-16 21:34:56

I know this question is asked numerous number of time and I have searched enough to find my results but didnt get success so far.

我知道这个问题被问了很多次,我搜索了很多次才找到了结果,但是到目前为止还没有成功。

Let say I have this type of array:

假设我有这种类型的数组:

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 ) @@@

From that array I want to omit/skip Array ( [macro_ratio] => [foodCategoryId] => 36 ) @@@ sort of elements means if [macro_ratio] => empty so I want to skip that whole element of array.

从这个数组中,我想省略/跳过数组([macro_ratio] => [foodCategoryId] => 36) @@@类型的元素意味着如果[macro_ratio] => empty,那么我想跳过数组的整个元素。

The code I am working on is:

我正在编写的代码是:

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);
                }
            }
        }

4 个解决方案

#1


1  

Both macro_ratio and foodCategoryId are keys, you can access then after the first loop. The if statement will check if macro_ratio is set and not empty. change your code to this and it should work

macro_ratio和foodCategoryId都是键,您可以在第一个循环之后访问它们。if语句将检查是否设置了macro_ratio而不是empty。将您的代码更改为这个,它应该可以工作

$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'];
    }           
 }
}

#2


1  

Try this if it works

如果可行的话,试试这个

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

    }

#3


1  

Hope this works... this is the simplest way.

希望这个工作…这是最简单的方法。

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

#4


1  

This is an alternative way of doing it. Filtering the array before the foreach loop. array_filter with a custom callback will do this.

这是另一种方法。在foreach循环之前过滤数组。使用自定义回调的array_filter将执行此操作。

This will return a filtered array by checking every element in the array to make sure the length of ['macro_ratio'] is greater than 0. Using strlen instead of empty will still allow values of 0.

通过检查数组中的每个元素以确保['macro_ratio']的长度大于0,这将返回一个过滤后的数组。使用strlen而不是empty仍然允许值为0。

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

#1


1  

Both macro_ratio and foodCategoryId are keys, you can access then after the first loop. The if statement will check if macro_ratio is set and not empty. change your code to this and it should work

macro_ratio和foodCategoryId都是键,您可以在第一个循环之后访问它们。if语句将检查是否设置了macro_ratio而不是empty。将您的代码更改为这个,它应该可以工作

$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'];
    }           
 }
}

#2


1  

Try this if it works

如果可行的话,试试这个

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

    }

#3


1  

Hope this works... this is the simplest way.

希望这个工作…这是最简单的方法。

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

#4


1  

This is an alternative way of doing it. Filtering the array before the foreach loop. array_filter with a custom callback will do this.

这是另一种方法。在foreach循环之前过滤数组。使用自定义回调的array_filter将执行此操作。

This will return a filtered array by checking every element in the array to make sure the length of ['macro_ratio'] is greater than 0. Using strlen instead of empty will still allow values of 0.

通过检查数组中的每个元素以确保['macro_ratio']的长度大于0,这将返回一个过滤后的数组。使用strlen而不是empty仍然允许值为0。

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