user3663481 user3663481 - 5 months ago 14
PHP Question

Array key value combine as comma separate for highchart

I have one

array
. My
array
as follows:-

Array
(
[High] => 44.153324447894
[Normal] => 9.9235739712854
[Low] => 1.2518347308218
)


I want to convert into as following array

array(
array('High', 44.153324447894),
array('Normal', 9.9235739712854),
array('Low', 1.2518347308218)
);


Please help me how to convert into above
array
.

Answer

"One-line" solution using array_map, array_keys and array_values functions:

$arr = ['High' => 44.153324447894, 'Normal' => 9.9235739712854, 'Low' => 1.2518347308218];
$new_arr = array_map(null, array_keys($arr), array_values($arr));

print_r($new_arr);

The output:

Array
(
    [0] => Array
        (
            [0] => High
            [1] => 44.153324447894
        )

    [1] => Array
        (
            [0] => Normal
            [1] => 9.9235739712854
        )

    [2] => Array
        (
            [0] => Low
            [1] => 1.2518347308218
        )
)

http://php.net/manual/en/function.array-map.php

An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function