2

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.

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);
                }
            }
        }
2
  • What is macro_ratio? is that a key? Commented Aug 24, 2015 at 0:48
  • Yes it is the key. print_r($value); contains all the above array output. Like: Array ( [macro_ratio] => 1.1 [foodCategoryId] => 37 ) Array ( [macro_ratio] => 1.1 [foodCategoryId] => 37 ) Array ( [macro_ratio] => 1.2 [foodCategoryId] => 2 ) Array ( [macro_ratio] => 1.2 [foodCategoryId] => 2 ) Array ( [macro_ratio] => 1.3 [foodCategoryId] => 3 ) Array ( [macro_ratio] => 1.3 [foodCategoryId] => 3 ) Array ( [macro_ratio] => [foodCategoryId] => 4 ) Commented Aug 24, 2015 at 0:49

4 Answers 4

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

    }
Sign up to request clarification or add additional context in comments.

3 Comments

output is: 1 ~~~ 2 ~~~
Is print_r($value2);? 1~~~ 2~~~ Output?
you should use something like: if ( isset($value['macro_ratio']) && !empty($value['macro_ratio']) ) { echo $value['foodCategoryId']; } then foreach loop again.
0

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

$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 Comments

Most welcome. I have improved the code a bit for readability.
Just a little and not very important trick: you don't need to use isset() because empty() also checks if the variable exists. php.net/manual/en/function.empty.php :P
0

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

1 Comment

output is: array(0) { }
0

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

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.