PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] Cannot remove null elements from nested array

 January 28, 2022     codeigniter, php     No comments   

Issue

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

Solution

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



Answered By - Always Sunny
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing