0

I have the following associative array:

Array
(
    [0] => Array
        (
            [0] => 18-Jul-16
            [1] => 29-Jul-15
            [2] => 2-Feb-16
            [3] => 3301
            [4] => 1800 Bimodel
            [5] => 5813
            [6] => 1 800 Bimodel Multi-Option Test
            [7] => Tested
            [8] => Shop Center
            [9] => Shop Services
            [10] => 864
            [11] => 20
            [12] => 884
            [13] => 0.75
            [14] => 0.2
            [15] => 0.49
            [16] => 429.6
            [17] => 47.3
            [18] => 382.3
            [19] => 
            [20] => Jonas John
        )

    [1] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
            [18] => 
            [19] => 
            [20] => 
        )
)

To filter out the empty array elements, I am using the following code:

            $arrData = array_map('array_filter', $arrData);
            $arrData = array_filter($arrData);

The above code removes all empty elements from the array. It also removes value at index 19 in the 0th array. I want it to remove array elements only if all the values of the array are empty. In my case only array at index 1 should be removed and the array at 0th index should not be removed (or any of its elements). I will always need 21 elements from each array. Can I do that selectively using php code ?

Thanks

4 Answers 4

1
 foreach($dataArray as $outerKey =>$innerArray) {
 $counter=0;         
 foreach($innerArray as $key=>$value){
      if(trim($value)==""){
        $counter++;
      } 
          if($counter==count($innerArray)
              unset($dataArray[$outerKey]);
    }
 }
Sign up to request clarification or add additional context in comments.

4 Comments

May be you should add unset of $dataArray element when $innerArray remains empty ?
can write another logic directly.. @krasipenkov updated it..directly accessed the key and then did unset it
Yeah but the wanted result is to remove outer array element only when all its inner elements are empty ... if I understood it right
just read the detailed explanation, will need to update the code.
0
// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return $value !== ''; }));

Comments

0

No magic just simply:

<?php
/**
$data contains (incoming) unfiltered value
*/
$data = [
    'keep1' => [
        0,1,2,3
    ]
    ,
    'drop1'=>[
        '',0,false,[]
    ]
    ,'keep2'=>[
        2,2,3,4
    ]
];

foreach ($data as $key => $innerArray) { //check for each element
    foreach ($innerArray as $innerValue) {
        if ( !empty($innerValue) ) {
            continue 2;//stop investigating at first non empty, we shoud keep this
        }
    }
    //all values in innerArray are empty, drop this
    unset($data[$key]);

}

print_r(array_keys($data));

Output:

Array
(
    [0] => keep1
    [1] => keep2
)

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

I think there is a really simply solution for this. I recently had the same issue. Just use

$$data = array_filter($$data, 'array_filter');

array_filter() removes elements from the array which bool convert to false. This includes - for example - each of [], null, '', 0, false.

What it does

  1. The first array_filter() takes each of the sub-arrays and throws it in the second array_filter().
  2. The second array_filter() removes every false element and returns the rest. Its important to notice that the returned array will become empty if every element converts to false.
  3. The first array_filter() takes the result from the second and checks against false. So, in case an empty array is returned the sub-array will be removed from the array.

To prove the case I used the data from cske above:

<?php

$data = [
    'keep1' => [0,1,2,3
    ],
    'drop1' => ['',0,false,[]
    ],
    'keep2' => [2,2,3,4
    ],
];
print_r(array_filter($data, 'array_filter'));

Output

Array
(
[keep1] => Array
    (
        [0] => 0
        [1] => 1
        [2] => 2
        [3] => 3
    )

[keep2] => Array
    (
        [0] => 2
        [1] => 2
        [2] => 3
        [3] => 4
    )

)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.