I'm looking for a way to filter array so all elements with key is specified are removed.
Here's an example of what I mean:
$x = array(
'a', // pass
'b::a', // pass
array('a'), // pass
array('a', 'b'), // pass
function() { // pass
return 'a';
},
'a' => 'b', // doesn't pass because key is specified
);
After remove_elements_with_key_specified($x)
...
array(5) {
[0]=> string(1) "a"
[1]=> string(4) "b::a"
[2]=> array(1) { [0]=> string(1) "a" }
[3]=> array(2) { [0]=> string(1) "a" [1]=> string(1) "b" }
[4]=> object(Closure)#1 (0) { }
}
How to do it? Would simple check that key is a string would be the best way?
is_int()
would work:
foreach($x as $k=>$v){
if(!is_int($k)){
unset($x[$k]);
}
}
From docs:
<?php
if (is_int(23)) {
echo "is integer
";
} else {
echo "is not an integer
";
}
var_dump(is_int(23));
var_dump(is_int("23"));
var_dump(is_int(23.5));
var_dump(is_int(true));
?>
The above example will output:
is integer
bool(true)
bool(false)
bool(false)
bool(false)
All of your elements have an implicit numeric key, so I guess what you wanna do here is something like:
foreach($x as $key => $value) {
if(!is_numeric($key) {
unset($x[$key]);
}
}
That should work.