0

Basically I have this array $code:

Array
(
    [1] => Array
        (
            [0] => FRANCE
            [1] => 26422
            [2] => 61748
            [3] => 698477678
        )

    [2] => Array
        (
            [0] => UNITED STATES
            [1] => 545
            [2] => 2648
            [3] => 55697455
        )

    [3] => Array
        (
            [0] => CANADA
            [1] => 502
            [2] => 1636
            [3] => 15100396
        )
    [4] => Array
        (
            [0] => GREECE
            [1] => 0
            [2] => 45
            [3] => 458
        )

I want to unset all the countries with $code[$key][1] == 0 so I tired this:

$code = array_filter($code, function (array $element) {
return !preg_match('(0)i', $element[1]);
});

But it returns all the countries unless the one that has in $code[$key][1] 0, Like this:

Array
(
    [1] => Array
        (
            [0] => FRANCE
            [1] => 26422
            [2] => 61748
            [3] => 698477678
        )

    [2] => Array
        (
            [0] => UNITED STATES
            [1] => 545
            [2] => 2648
            [3] => 55697455
        )

Any how I can achieve this? Thanks!

2
  • 1
    Why using preg. In this way you are filtering all countries which code contains 0. Use simple == instead Commented Apr 14, 2014 at 16:44
  • Why not just return $element[1] !== 0;? (or return $element[1] !== "0"; in case of a string). In your sample data this will remove only GREECE. The regular expression would ^0$ but don't use regular expressions here. Commented Apr 14, 2014 at 16:44

2 Answers 2

2

Without regex:

$code = array_filter($code, function (array $element) {
return ($element[1] !== 0);
});

With regex (you need to use anchors):

$code = array_filter($code, function (array $element) {
return !preg_match('/^0$/', $element[1]);
});

However, I’ll recommend a simple foreach loop instead of array_filter:

foreach($code as $key => $val){
    if($val[1] === 0) unset($code[$key]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1. Anyway ,preg_match() isn't required for this task. The first one should do it. I would change it to $element[1] !== 0;, though :)
@AmalMurali I’ve given the regex solution simply in order to show what went wrong with the original attempt by the author of the question. And I’ve made the change that you’ve suggested here. Thank you very much.
I know, @Sharanya. That was just meant to be an "FYI" comment for future readers. I see you've added a foreach solution as well. That's the best one to use here, IMO.
1

If I understand, you are trying to remove only Greece, it should be as simple as:

$code = array_filter($code, function (array $element) {
    return $element[1] != 0
});

The regular expression you are using will remove every country where the key has a 0 in the value, that will exclude also 502 in your example.

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.