Daniii Posted December 19, 2020 Share Posted December 19, 2020 $url = ""; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_URL, $url); $getdata = curl_exec($curl); $Object = json_decode($getdata); $myObjectMap1 = $Object->result; $datapoints = array(); foreach($myObjectMap1 as $key => $item): $dataPoints[] = array( 'label' => $item->Users[$key]->UserObject->UserName ,'y' => $item->Users[$key]->Amount ); endforeach; print_r($dataPoints); echo json_encode($dataPoints, JSON_FORCE_OBJECT); ?> Hello in this Array i only get User1 but not the others. How can i fix this { "result": [ { "ID": 1, "Users": [ { "UserObject": { "UserName": "User1", "-": { "ID": 1 }, "0": "0" }, "User": "User1", "Amount": 57654456 }, { "UserObject": { "UserName": "User2", "-": { "ID": 1 }, "0": "0" }, "User": "User2", "Amount": 57654456 }, { "UserObject": { "UserName": "User3", "-": { "ID": 1 }, "0": "0" }, "User": "User3", "Amount": 57654456 } ], "Reached": false, "IsActive": true } ], "error": false, "version": 1 } Quote Link to comment https://forums.phpfreaks.com/topic/311895-help-with-array/ Share on other sites More sharing options...
requinix Posted December 19, 2020 Share Posted December 19, 2020 foreach($myObjectMap1 as $key => $item): $key is only valid for use on $myObjectMap1. Using it on Users $item->Users[$key] will not work correctly. Hint: if you have two arrays then you probably need two loops. Quote Link to comment https://forums.phpfreaks.com/topic/311895-help-with-array/#findComment-1583270 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.