PHP Strip count value from Array
I have a ldap request that returns an array, however the returned array is full of "counts" and odd pointers, as this application is an API designed to be used with Javascript on mobile devices, the less text there is the better.
What is the most efficient way to strip out all the values that don't have the key count? As well as the ones that just seem to be saying what the keys are (eg [0] => cn)?
Array
(
[status] => OK
[count] => 4
[results] => Array
(
[count] => 3
[0] => Array
(
[cn] => Array
(
[count] => 1
[0] => James Bee
)
[0] => cn
[umanprimaryou] => Array
(
[count] => 1
[0] => Awesome School
)
[1] => umanprimaryou
[ou] => Array
(
[count] => 2
[0] => School of Awesome
[1] => Faculty of Engineering
)
etc...
Aiming for
Array
(
[status] => OK
[results] => Array
(
[0] => Array
(
[cn] => Array
(
[0] => James Bee
)
[umanprimaryou] => Array
(
[0] => Awesome School
开发者_开发知识库 )
[ou] => Array
(
[0] => School of Awesome
[1] => Faculty of Engineering
)
etc...
For further explanation I am wanting to unset all the [count] => value pairs and if possible the values such as [0] => cn in the results array.
Untested code:
function strip_count(array $arr) {
foreach ($arr as $key => $value) {
if (is_array($arr[$key])) {
strip_count($arr[$key]);
} else {
if ($key == 'count') {
unset($arr[$key]);
}
}
}
}
Edit: I wrote this before you edited your question, but it shouldn't be too hard to modify this recursive function to strip out the other things you wish to remove as well.
Might wanna look into using array_walk_recursive and check if $key == 'count'
in your user defined function.
精彩评论