Keep the true ARRAY OBJECT between Javascript -to- PHP via Ajax? -
i want post javascript array object via ajax php. @ php end, still want use object true array. below approach far still not successful in terms of using incoming object array @ php side. can parse string.
javascript:
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 } } then, javascript/jquery send via ajax:
$.ajax({ type: "post", url: "ajax.php", datatype: "json", data: { "data" : json.stringify(myobj) } }).success(function( response ) { alert(response); }); then parse @ php:
$data = 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] ); - any of above
echoreturns blanks, browser.
but when i:
$data = json_decode( $_post["data"] ); $to_string = print_r( $data, true ); //<-- used these instead echo json_encode( $to_string ); //<-- used these instead .. returns following big strings browser:
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 ) ) - so seems data there, how can parse data in proper truely
arraymanner @phpend, please?
pass second argument true, convert data in array
$data = json_decode( $_post["data"] , true);
Comments
Post a Comment