Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
    
Can you show an example of your desired array output? – stevecomrie May 13 '11 at 21:17
    
Seems you're prematurely optimizing something that may not be a real issue. Do you know that these extra fields are a serious degradation to your application? – JaredMcAteer May 13 '11 at 21:19
    
@OriginalSyn they are not necessarily a degradation, but as the devices connecting to the api will be mobile, the smaller I can make the data the better, and the count field is not used at all on the related app. @SteveComie, see post – Pez Cuckow May 13 '11 at 21:20
    
I don't understand what you're wanting to do either...are you wanting to remove all 'count' => value elements? – Crayon Violent May 13 '11 at 21:23
    
Check out this answer. stackoverflow.com/questions/1708860/… – Anthony Jack May 13 '11 at 21:27
up vote 3 down vote accepted

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.

share|improve this answer
1  
Would this not need to be passed as a reference? Or does unset work on the given array? Also how could I add the stripping of [0] => cn's? – Pez Cuckow May 13 '11 at 21:26
    
"Changing the values of the array directly is possible since PHP 5 by passing them by reference" - php.net/manual/en/language.types.array.php – Lotus Notes May 13 '11 at 21:31

Might wanna look into using array_walk_recursive and check if $key == 'count' in your user defined function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.