0

We have a multidimensional array that currently looks like this.

[totalsByYardHand] =>
  [Jon Doe] =>
    [Delivieries] => 6
    [Pickups] => 5
    [Errors] => 8
  [Fred] =>
    [Delivieries] => 6
    [Pickups] => 5
    [Errors] => 8

Since we are passing this array off to JavaScript and want to maintain the order we are needing this to be formatted correctly. We are wanting to take the array and format it as so.

Array
(
[totalsByYardHand] => Array
    (
        [0] => Array
            (
                [0] => Jon Doe
                [1] => Array
                    (
                        [0] => Array
                            (
                                [0] => Deliveries
                                [1] => 6
                            )

                        [1] => Array
                            (
                                [0] => Pickup
                                [1] => 6
                            )

                        [2] => Array
                            (
                                [0] => Errors
                                [1] => 1
                            )

                    )

            )

        [1] => Array
            (
                [0] => Fred
                [1] => Array
                    (
                        [0] => Array
                            (
                                [0] => Deliveries
                                [1] => 6
                            )

                        [1] => Array
                            (
                                [0] => Pickup
                                [1] => 6
                            )

                        [2] => Array
                            (
                                [0] => Errors
                                [1] => 1
                            )
                    )
            )
    )
)

The closest attempt we made was a recursive function like so, but cannot figure out how to add the array keys from the original array into the newly formatted array.

function array_values_recursive($array)
{
    $array = array_values($array);
    for ($i = 0, $n = count($array); $i < $n; $i++) {
        $element = $array[$i];
        if (is_array($element)) {
            $array[$i] = $this->array_values_recursive($element);
        }
    }

    return $array;

}
4
  • 1
    Maybe you can use JSON and adapt your javascript accordingly? Commented Aug 26, 2016 at 14:47
  • formatted correctly Your second example is not correctly formatted. Lookup this part [0] => Jon Doe and the next line [0] => i thing this wont work Commented Aug 26, 2016 at 14:49
  • An array can have an subarray as value or an string as value, but not both! Commented Aug 26, 2016 at 14:52
  • 1
    Fixed the formatting of the array. Hope this clarifies what we are looking for. Commented Aug 26, 2016 at 15:17

2 Answers 2

1

This was a lot of fun to make! I am not good at recursion, so I love a challenge! let me know if this is what you want.

$testArray = [
    'totalsByYardHand' => [
        'Jon Doe' => [
            'Deliveries' => 6,
            'Pickups' => 5,
            'Errors' => 8
        ],
        'Fred' => [
            'Deliveries' => 8,
            'Pickups' => 5,
            'Errors' => 6
        ],
        'Mary Jane' => [
            'Deliveries' => 1,
            'Pickups' => 2,
            'Errors' => 4
        ],
    ]
];

function customArrayFormat($array) {
    $newArr = [];
    $arrayKeys = array_keys($array);
    for ($i=0; $i < count($array); $i++) { 
        $newArr[$i] = [
            0 => $arrayKeys[$i],
            1 => (is_array($array[$arrayKeys[$i]]) ? customArrayFormat($array[$arrayKeys[$i]]) : $array[$arrayKeys[$i]])
        ];
    }

    return $newArr;
}

print_r(customArrayFormat($testArray));

Outputs:

Array
(
    [0] => Array
        (
            [0] => totalsByYardHand
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => Jon Doe
                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Deliveries
                                            [1] => 6
                                        )

                                    [1] => Array
                                        (
                                            [0] => Pickups
                                            [1] => 5
                                        )

                                    [2] => Array
                                        (
                                            [0] => Errors
                                            [1] => 8
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [0] => Fred
                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Deliveries
                                            [1] => 8
                                        )

                                    [1] => Array
                                        (
                                            [0] => Pickups
                                            [1] => 5
                                        )

                                    [2] => Array
                                        (
                                            [0] => Errors
                                            [1] => 6
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [0] => Mary Jane
                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Deliveries
                                            [1] => 1
                                        )

                                    [1] => Array
                                        (
                                            [0] => Pickups
                                            [1] => 2
                                        )

                                    [2] => Array
                                        (
                                            [0] => Errors
                                            [1] => 4
                                        )

                                )

                        )

                )

        )

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

9 Comments

We have tried json_encode and JSON.parse. The problem with this is it parses the json into a JavaScript object not an array and the order of the object is not guaranteed in JavaScript. We need it to be a JavaScript array to guarantee the order matches the array from PHP.
Why does the order need to be sustained? I feel like the other parts of your code should not rely on order that much
We are passing data to google charts and need things such as dates and other data to always appear in the same order.
ok, is the data in your question all that needs to be transferred? Or should I write a recursive function?
Our array we are passing is several levels deep so I would think a recursive function would be best. We started to write one in the example above, but couldn't quite get it formatted correctly.
|
1

I think rewriting the array might not be so clever ... the function now will just make every (key,value) pair in the original into a [key,value] array.

function array_values_recursive($array)
{
    $ret = array();
    foreach($array as $key => $value) {
        if (is_array($value)) {
            $ret[] = array($key, $this->array_values_recursive($value));
        } else {
            $ret[] = array($key, $value);
        }
    }
    return $ret;
}

also, when the array has numerical keys, it will work nicely with json_encode, because it keeps arrays as arrays in those cases.

4 Comments

Exact same thing!
@HurricaneDevelopment Apparently I took too long to notice your edit in time... It's not as if you had this version of your answer an hour ago
Haha text doesn't convey my tone. I'm not upset, just kinda funny. No worries. For all I know, you thought of it before I made my edit.
@HurricaneDevelopment Fair enough. You definitely deserve the solution mark for the effort you put in ;o) (but I must say, I like foreach better, nothing to gain from for-looping over the array, I believe)

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.