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 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. 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 Doh! I have changed it to just encode the url now... but still the same error... 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); 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.. Link to comment https://forums.phpfreaks.com/topic/293163-json_encode-returning-null/#findComment-1499987 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.