2

I have an array, $row2.

In $row2 two arrays are present. The output of $row2 is:

Array
(
    [0] => Array
        (
            [Proposal_id] => 9
            [row] => 1
            [col1] => 2
            [col2] => 2
            [col3] =>
            [col4] =>
            [col5] =>
            [Type] => customtbl
            [Invoice_term] =>
            [Qoute] =>
            [Rate_per_hour] =>
            [Total] =>
        )

    [1] => Array
        (
            [Proposal_id] => 9
            [row] => 2
            [col1] => 3
            [col2] => 4
            [col3] =>
            [col4] =>
            [col5] =>
            [Type] => customtbl
            [Invoice_term] =>
            [Qoute] =>
            [Rate_per_hour] =>
            [Total] =>
        )

)

I want to remove null elements from the array, but I can't do this.

I tried the following methods:

array_filter($row2);
array_filter($row2, function($var){return !is_null($var);});
array_diff($rows2, array("null", ""));
6
  • 1
    Try $row2 = array_map('array_filter', $row2); Commented Mar 20, 2018 at 17:24
  • @billyonecan that works, altho could have unexpected results as it will also remove zeros. Commented Mar 20, 2018 at 17:32
  • @billyonecan please post your comment as an answer to this question Commented Mar 20, 2018 at 17:35
  • not working for me btw thanku for your feedback Commented Mar 20, 2018 at 17:51
  • Possible duplicate of How to remove empty values from multidimensional array in PHP? Commented Apr 22, 2018 at 7:36

2 Answers 2

1

I've an one liner solution to filter out null values from multidimensional array if you use array_map along with array_filter,

    $array = [
    ['Proposal_id' => 9,
     'row' => 1,
     'col1' => 2, 
     'col2' => 2, 
     'col3' => null,
     'col4' => null,
     'col5' =>  null,
     'Type' => 'customtbl',
     'Invoice_term' => null,  
     'Qoute' => null,
     'Rate_per_hour' => null,  
     'Total' => null,
    ],
    [   
        'Proposal_id' => 9,
        'row' => 1,
        'col1' => 2, 
        'col2' => 2 ,
        'col3' => null,
        'col4' => null,
        'col5' =>  null,
        'Type' => 'customtbl',
        'Invoice_term' => null,  
        'Qoute' => null,
        'Rate_per_hour' => null,  
        'Total' => null,
    ]
];
$af = array_filter(array_map('array_filter', $array));
print '<pre>';
print_r($af);
print '</pre>';

Output:

 Array
(
    [0] => Array
        (
            [Proposal_id] => 9
            [row] => 1
            [col1] => 2
            [col2] => 2
            [Type] => customtbl
        )

    [1] => Array
        (
            [Proposal_id] => 9
            [row] => 1
            [col1] => 2
            [col2] => 2
            [Type] => customtbl
        )

)

DEMO : https://eval.in/975240

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

4 Comments

@dani have you seen the demo? It should work..why it is not working for you? can you explain?
your code is working with your array but not work with me
i'm fetching the data from the database may be thats way not working
Yeah that could be a reason. But I've posted the answer based on your array structure on the question's code. Best of luck..
1

Use array_filter within array_map. Just remember you may want to run a different conditional rather than if($value) as this will validate against not only nulls, but zeros and empty strings.

array_map(function($var) {
    return array_filter($var, function($value) {
        if ( !is_null($value) )
            return $value;
    });
},$row2);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.