0

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.

5
  • Can you show an example of your desired array output? Commented May 13, 2011 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? Commented May 13, 2011 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 Commented May 13, 2011 at 21:20
  • I don't understand what you're wanting to do either...are you wanting to remove all 'count' => value elements? Commented May 13, 2011 at 21:23
  • Check out this answer. stackoverflow.com/questions/1708860/… Commented May 13, 2011 at 21:27

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
"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
2

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

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.