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
$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)
);
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With