Keep the true ARRAY OBJECT between Javascript -to- PHP via Ajax?

louadesoov

New Member
I want to post Javascript Array Object via Ajax to PHP. Then at PHP end, i still want to use that object as an true Array. Below is my approach so far still not successful in terms of using the incoming object as an \[code\]Array\[/code\] at PHP side. I can just parse it as the \[code\]string\[/code\].Javascript:\[code\]var myObj = { fred: { apples: 2, oranges: 4, bananas: 7, melons: 0 }, mary: { apples: 0, oranges: 10, bananas: 0, melons: 0 }, sarah: { apples: 0, oranges: 0, bananas: 0, melons: 5 } }\[/code\]Then, Javascript/JQuery to send via AJAX:\[code\]$.ajax({ type: "POST", url: "ajax.php", dataType: "JSON", data: { "data" : JSON.stringify(myObj) }}).success(function( response ) { alert(response);});\[/code\]Then parse at PHP:\[code\]$data = http://stackoverflow.com/questions/15832737/json_decode( $_POST["data"] );echo json_encode( $data["fred"] );echo json_encode( $data["fred"]["apples"] );echo json_encode( $data["fred"]->apples );echo json_encode( $data[1] );\[/code\]
  • Any of above \[code\]echo\[/code\] returns blanks, back to the browser.
But when i:\[code\]$data = http://stackoverflow.com/questions/15832737/json_decode( $_POST["data"] );$to_string = print_r( $data, true ); //<-- Used these insteadecho json_encode( $to_string ); //<-- Used these instead\[/code\].. it returns following big strings back to the browser:\[code\]stdClass Object( [fred] => stdClass Object ( [apples] => 2 [oranges] => 4 [bananas] => 7 [melons] => 0 ) [mary] => stdClass Object ( [apples] => 0 [oranges] => 10 [bananas] => 0 [melons] => 0 ) [sarah] => stdClass Object ( [apples] => 0 [oranges] => 0 [bananas] => 0 [melons] => 5 ))\[/code\]
  • So seems data is there, but how can i parse this data as in the proper truely \[code\]ARRAY\[/code\] manner at \[code\]PHP\[/code\] end, please?
 
Top