0

I have an array, I just print it as-
print_r($data);
It shows output as-

Array
(
    [0] => Array
        (
            [0] => Title
            [1] => Featured Image
            [2] => Catagories
            [3] => Tags
            [4] => Content
        )

    [1] => Array
        (
            [0] => title 1
            [1] => img1.jpg
            [2] => cat 1
            [3] => tag 1
            [4] => post 1 content
        )

    [2] => Array
        (
            [0] => title 2
            [1] => img2.jpg
            [2] => cat2
            [3] => tag 2
            [4] => post 2 content
        )

    [3] => Array
        (
            [0] => title 3
            [1] => img3.jpg
            [2] => cat3
            [3] => tag3
            [4] => post 3 content
        )

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

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

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

)  

I just want to remove blank or null values from array.
I tried array_dif() and array_filter() but still I couldn't remove null values.
How is this possible?
Thanks.

4
  • try to read al larray and generate a new array without null values Commented Feb 6, 2017 at 7:06
  • stackoverflow.com/questions/3654295/remove-empty-array-elements Commented Feb 6, 2017 at 7:07
  • Loop over the main array and filter the array value using the array_filter; Commented Feb 6, 2017 at 7:09
  • So you want to remove elements 4, 5, 6 from main array right? Commented Feb 6, 2017 at 7:24

2 Answers 2

1

You can loop through the array, look for empty variables and use unset to remove them.


This code will loop through and check if the length of the first value in each array is at least one character long and unset it if its not.

<?php
foreach($data as $key => $value) {
    if(!isset($value[0][0]))
        unset($data[$key]);
}

This code will loop through the array in a similar way, except to check every value of every array to determine if its parrent array should be kept or left to be unset.

<?php
foreach($data as $key => $values) {
    foreach($values as $value) {
        if(isset($value[0]))
            continue 2;
    }
    unset($data[$key]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should use array_filter with a function that also runs array_filters against the subarrays and returns false if the filtered subarrays become empty.

<?php

$array = Array(
    Array(1, 2, 3),
    Array(null, null, null),
    Array(false, false, false),
    Array(3, 2, 1)
);

$filtered = array_filter($array, function($elem) {
    return count(array_filter($elem));
});

print_r($filtered);

?>

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.