3

I have an array like so

array(
      1=>hello,
      2=>foo,
      3=>192,
      4=>keep characters AND digits like a1e2r5,
);

All I want to do is to remove rows containing digits ONLY (3=>192), and return an array like this one :

array(
      1=>hello,
      2=>foo,
      3=>keep characters AND digits like a1e2r5,
);

I tried with array_filter but didn't get it work. Can someone show me how to do? Thanks

4 Answers 4

12
$data = array( 1 => "hello", 
               2 => "foo", 
               3 => "192", 
               4 => "keep characters AND digits like a1e2r5", 
             );

$result = array_filter( $data, 
                        function($arrayEntry) { 
                            return !is_numeric($arrayEntry);
                        }
                      );

Or using slightly more modern PHP, with arrow functions:

$result = array_filter( $data, 
                        fn($arrayEntry) => !is_numeric($arrayEntry)
                      );
Sign up to request clarification or add additional context in comments.

2 Comments

Be careful using this solution. It deletes the rows with only numbers, but doesn't adjust any of the numeric indices accordingly. Of course that might be what you want, in which case nevermind.
And if you do want numeric indexes resetting, just wrap it in a call to array_values()
1

You could use a loop and the intval function.

$filteredArray = array();
foreach($array as $element){
    //this works because PHP is weakly typed
    if(intval($element) != $element){
        $filteredArray[] = $element;
    }
}

Comments

0

Are you sure you were using array_filter correctly? It's the best solution for your problem.

// named callback for backwards compatibility, but use an anonymous function
// if you have a high enough php version.
function callback($item) { return !is_numeric($item); }

$result = array_filter($a, 'callback');
print_r($result);

// optional - causes numeric keys to be in order
$result = array_values($result);
print_r($result);

Output using example input from question as $a:

Array
(
    [1] => hello
    [2] => foo
    [4] => keep characters AND digits like a1e2r5
)

Array
(
    [1] => hello
    [2] => foo
    [3] => keep characters AND digits like a1e2r5
)

Comments

0

I'm surprised no one mentioned this in any of the answers: using numeric tests is not a total solution. Using numeric tests will remove some elements containing non-digit characters if they are evaluated as numeric. Specifically, {e, -, .}

$data=array(
  1=>'hello',
  2=>'foo',
  3=>'192',
  4=>'keep characters AND digits like a1e2r5',
  5=>'1.4',
  6=>'-42',
  7=>'1e2',
  8=>'1.23e4',
); 
function callback1($arrayEntry) { 
    return !is_numeric($arrayEntry);
}
$result = array_filter( $data, 'callback1');

echo '<pre>';
print_r($result);

echo '<hr>';
function callback2($arrayEntry) { 
    return !preg_match('/^[0-9]+$/', $arrayEntry);
}
$result = array_filter( $data, 'callback2');
print_r($result);

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.