Pregunta

I have a PHP array that is created from a simple XML object and looks like the following example (hard-coded for demoing):

$arr = array("item1"=>"53","item2"=>"20","item3"=>"7","item4"=>"4","item4"=>"2","item6"=>"2","item7"=>"1");

I would like to use this array to create a chart using Highcharts which requires a different format (see below). How can I convert the content of my array to match this format ?

[['item1',53],['item2',20],['item3',7],['item4',4],['item5',2],['item6',2],['item7',1]]

Many thanks for any help with this, Mike.

¿Fue útil?

Solución

Just try with:

$output = json_encode(array_map(function($key, $value){
    return array($key, (int) $value);
}, array_keys($arr), array_values($arr)));

Output:

string '[["item1",53],["item2",20],["item3",7],["item4",2],["item6",2],["item7",1]]' (length=75)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top