PHP two dimensional array to Javascript array
I had php multi dimensional array
Array
(
[0] => Array
(
[WorkHrs] => 9826
[Focus_Date] => 2010-02-10
)
[1] => Array
(
[WorkHrs] => 9680
[Focus_Date] => 2010-02-11
)
)
开发者_如何学Python
and I want to convert it in Javascript to
myArray = [['2010-02-10', 9826],['2010-02-11', 9680]];
$jsArray = array();
foreach($myArray as $array) {
$jsArray[] = array($array['Focus_Date'], (int) $array['WorkHrs']);
}
echo json_encode($jsArray);
echo json_encode(array_map(array_values, $arr));
EDIT: To get it in the specified order:
function to_focus_work_array($arr)
{
return array($arr['Focus_Date'], $arr['WorkHrs']);
}
echo json_encode(array_map('to_focus_work_array', $arr));
json_encode
That's pretty much exactly what json_encode does. Input is a PHP-array (other datatypes accepted), output is what you describe.
have you tried the json_encode()? refer to http://php.net/manual/en/function.json-encode.php
精彩评论