Staggan Posted December 18, 2014 Share Posted December 18, 2014 Hello I am having an issue with encoding an array to JSON, my array seems to be formed correctly but when I try and echo the json_encode version I get "null". I echo the JSON error code and it is "0" indicating no issue.. I have read that non UTF-8 characters can be an issue, but this is simple data, no strange characters... but I added the utf8_encode anyway.... Here is my code: $dataArray = array( 'title' => 'Test Item', 'icon_url' => 'http://url/api/testimage.jpg', 'item_id' => '12345', 'expiration' => 600 ); utf8_encode($dataArray); $responsArray = array( 'success' => '1', 'data' => $dataArray ); utf8_encode($responsArray); header('Content-type: application/json'); echo print_r($responsArray); echo json_encode ($responseArray); echo json_last_error(); Any thoughts would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/293163-json_encode-returning-null/ Share on other sites More sharing options...
Ch0cu3r Posted December 18, 2014 Share Posted December 18, 2014 Why are you passing the array to utf8_encode? It expects a string not an array. Quote Link to comment https://forums.phpfreaks.com/topic/293163-json_encode-returning-null/#findComment-1499970 Share on other sites More sharing options...
Staggan Posted December 18, 2014 Author Share Posted December 18, 2014 (edited) Doh! I have changed it to just encode the url now... but still the same error... Edited December 18, 2014 by Staggan Quote Link to comment https://forums.phpfreaks.com/topic/293163-json_encode-returning-null/#findComment-1499973 Share on other sites More sharing options...
Barand Posted December 18, 2014 Share Posted December 18, 2014 $responsArray = array( .... echo json_encode ($responseArray); Quote Link to comment https://forums.phpfreaks.com/topic/293163-json_encode-returning-null/#findComment-1499975 Share on other sites More sharing options...
Staggan Posted December 18, 2014 Author Share Posted December 18, 2014 Doh! Thanks, wood for trees comes to mind.. Quote Link to comment https://forums.phpfreaks.com/topic/293163-json_encode-returning-null/#findComment-1499987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.