1

I have the following multidimensional array:

$subregion = [
    [
        "subregion_id"   => "8",
        "subregion_name" => "NORTH_AMERICA",
        "subregion_abbr" => "US"
    ],
    [
        "subregion_id"   => "9",
        "subregion_name" => "NORTH_AMERICA",
        "subregion_abbr" => "CA"
    ],
    [
        "subregion_id"   => "6",
        "subregion_name" => "WE",
        "subregion_abbr" => "South"
    ],
    [
        "subregion_id"   => "0",
        "subregion_name" => null,
        "subregion_abbr" => null
    ],
    [
        "subregion_id"   => "5",
        "subregion_name" => "WE",
        "subregion_abbr" => "North"
    ],
    [
        "subregion_id"   => "0",
        "subregion_name" => null,
        "subregion_abbr" => null
    ]
];

I want to unique that array by its values so I have test the following (from this post):

$subregion = array_map("unserialize", array_unique(array_map("serialize", $subregion)));
$subregion = array_intersect_key($subregion, array_unique(array_map('serialize', $subregion)));
$subregion_unique = array_unique($subregion, SORT_REGULAR);

All of them work (tried one by one) and produces the same output:

$subregion = [
    [
        "subregion_id"   => "8",
        "subregion_name" => "NORTH_AMERICA",
        "subregion_abbr" => "US"
    ],
    [
        "subregion_id"   => "9",
        "subregion_name" => "NORTH_AMERICA",
        "subregion_abbr" => "CA"
    ],
    [
        "subregion_id"   => "6",
        "subregion_name" => "WE",
        "subregion_abbr" => "South"
    ],
    [
        "subregion_id"   => "5",
        "subregion_name" => "WE",
        "subregion_abbr" => "North"
    ],
    [
        "subregion_id"   => "0",
        "subregion_name" => null,
        "subregion_abbr" => null
    ]
];

I want to get rid of the null values on the array because them should not be there. So I have tried this other workaround (found here):

private function is_not_null($var)
{
    return !is_null($var);
}

$unique = array_filter($subregion_unique, 'is_not_null');

But surprise the output of $unique is exactly the same as before, why? What I am missing here?

NOTE I forgot to mention that should be compatible with PHP 5.3.10+

Update: test1

Following @abracadaver instructions this is what I've tried:

$unique = array_map('unserialize', array_unique(array_map('serialize', $subregion)));
$unique = array_map(function($v) { return array_filter($v, 'is_not_null'); }, $unique);

I have changed the function is_not_null($var) above to return result in this way:

return null !== $var;

The result is an array with null values.

PS: Maybe the title is not accurate and the post is tempted to be closed or marked as duplicate, if so please suggest me a better title or change by yourself if you can.

5
  • $subregion_unique is? Anyway, the problem is that you are not accessing your 2-Dimensional array properly in the function is_not_null Commented Oct 26, 2016 at 20:29
  • @Aaron the last line on my test $subregion_unique = array_unique($subregion, SORT_REGULAR); Commented Oct 26, 2016 at 20:30
  • Have a look at stackoverflow.com/questions/27447923/… Commented Oct 26, 2016 at 20:31
  • @Aaron is not what I am doing already at is_not_null() ? Commented Oct 26, 2016 at 20:33
  • 1
    Nope. You need to check something like !is_null($var['property']) instead. Commented Oct 26, 2016 at 20:34

3 Answers 3

2

Not sure why you have all that code. This one line gets a unique array:

$unique = array_map("unserialize", array_unique(array_map("serialize", $subregion)));

Then to filter out the null with your current function:

$unique = array_map(function($v) { return array_filter($v, 'is_not_null'); }, $unique);

If you don't care if the 0 is removed then it's even shorter and you don't need your function:

$unique = array_map(function($v) { return array_filter($v); }, $unique);

Then you know you'll have an empty child array, so you can filter it:

$unique = array_filter($unique);

The SORT_REGULAR makes no difference here, but for when it may, or if you need a different flag, you would just add it in the above array_unique():

$unique = array_map("unserialize",
                    array_unique(array_map("serialize", $subregion), SORT_REGULAR));
Sign up to request clarification or add additional context in comments.

2 Comments

Check my Update: test1 in OP, isn't working for me.
Probably need a new question, it obviously works 3v4l.org/dQkaE
0

This code will help you get rid off the null:

foreach($subregion_unique as $key => $item) {
    foreach($item as $itemKey => $value) {
        if($value === null) {
            unset($subregion_unique[$key][$itemKey];
        }
    }
}

Comments

0

php >=5.3 use it in this way

$unique = array_map(function($a){
                 return array_filter($a); 
              },$subregion);

to filter each subarray

or just

foreach($subregion as &$a){
   $a=array_filter($a);
}

if the "subregion_id"="0" can be skipped

6 Comments

Removes "0" as well.
@AbraCadaver Good Point, but the entry with "0" has no value, so it can be removed. Or is Id=0 a real id ;)
Dunno. Based on the key subregion_id I assumed it might be important.
@JOUM is this compatible with PHP 5.3.10+? I forgot to mention that on the OP
Anonymous functions available since 5.3.0.
|

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.