2

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]];
hakre
200k55 gold badges454 silver badges868 bronze badges
asked Sep 27, 2010 at 5:45
5
  • possible duplicate of How to encode 2 dimentional PHP array to Javascript Array? Commented Sep 27, 2010 at 5:50
  • @deceze, no, that's different. Here, the inner arrays are associative. Commented Sep 27, 2010 at 5:52
  • @Matthew Sorry, you're right, but it's very related at least. :) Commented Sep 27, 2010 at 5:55
  • @mapet You should point out the specific problem you're struggling with. A question with this title pops up about twice a week, hence the one-word json_encode answers 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. Commented Sep 27, 2010 at 6:03
  • @deceze sorry next time i will.. tnx Commented Sep 27, 2010 at 6:04

5 Answers 5

9
$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
Sign up to request clarification or add additional context in comments.

Comments

4
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

2 Comments

+1 for array_values(). Attempting to encode associative arrays results in JSON objects, therefore you need to remove the associative keys.
This won't give the values in the desired order though. :)
1
answered Sep 27, 2010 at 5:49

1 Comment

hi david i try it before but the example of php.net is not two dimensional array :(
0

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

1 Comment

No, if he uses json_encode directly, the output will be an array of JSON objects.
0

have you tried the json_encode()? refer to http://php.net/manual/en/function.json-encode.php

answered Sep 27, 2010 at 5:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.