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日
)
)
and I want to convert it in Javascript to
myArray = [['2010-02-10', 9826],['2010-02-11', 9680]];
5 Answers 5
$jsArray = array();
foreach($myArray as $array) {
$jsArray[] = array($array['Focus_Date'], (int) $array['WorkHrs']);
}
echo json_encode($jsArray);
answered Sep 27, 2010 at 5:50
Jacob Relkin
164k34 gold badges352 silver badges321 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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));
answered Sep 27, 2010 at 5:50
Matthew Flaschen
286k53 gold badges525 silver badges554 bronze badges
answered Sep 27, 2010 at 5:49
Quentin
949k137 gold badges1.3k silver badges1.4k bronze badges
1 Comment
mapet
hi david i try it before but the example of php.net is not two dimensional array :(
That's pretty much exactly what json_encode does. Input is a PHP-array (other datatypes accepted), output is what you describe.
answered Sep 27, 2010 at 5:49
Alexander Sagen
4,0381 gold badge21 silver badges15 bronze badges
1 Comment
Matthew Flaschen
No, if he uses
json_encode directly, the output will be an array of JSON objects.have you tried the json_encode()? refer to http://php.net/manual/en/function.json-encode.php
answered Sep 27, 2010 at 5:50
jebberwocky
1,0873 gold badges11 silver badges24 bronze badges
Comments
default
json_encodeanswers and my automatic duplicate-reflex. If you have already tried that, point out what didn't work or what's special about your case. Not everybody is going to read your code in such detail as to spot the intricate differences (we're all staring at code all day already), so pointing them out explicitly will get you better answers.