Conner Burnett Conner Burnett - 3 months ago 9
PHP Question

Formatting PHP array for Javascript

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;

}

Answer

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
                                        )

                                )

                        )

                )

        )

)
Comments