Can anyone explain this code?

Ok, bit of background first. I used to work in IT and left 13 years ago. I mostly worked with php, mysql and jQuery/javascript. I’m now in another industry but have come across a problem at work that could be resolved with a fairly simple webpage. It will query our content management system for appointment information, which I will then do various things with and display it in the way we need it.

The system has a REST Api which I have access to. The company that makes the system provide example code, but no real support. I am trying to get my head around what they are doing in the code below. The api returns json appointments, which each contain id, start, end, resource, patient_id and a few other things. This is put into an array which is then looped through, and that’s where I’m confused. I haven’t seen the notation of the double [] before and I just can’t get into my head what they are trying to achieve.

$cloud_appointments_list = json_decode(sendRequestToCloud($token, $appointment_url, array2json(array()), 'GET')); //Send GET request to Cloud and immediately decode the json into array
    $cloud_appointments_array = array();

   
        $cloud_appointments = $cloud_appointments_list->results; //Put results array into variable for easier handling

        foreach ($cloud_appointments as $cloud_appointment) { //Loop through retrieved appointments
            // Store what information is needed
            $cloud_appointments_array[$cloud_appointment->url]['start'] = $cloud_appointment->start;
            $cloud_appointments_array[$cloud_appointment->url]['end'] = $cloud_appointment->end;
        }

return $cloud_appointments_array; //Return the finished appointment list
      

Ultimately I’m looking for the function to return an array of appointments, with the relevant info - which I can then work through elsewhere. I cannot get my head around what is being done in the foreach loop.

Any help greatly appreciated.

edit - literally as I’ve just read this post over, I may have understood it. Are they setting the key/index of $coud_appointments_array to the url property of the $cloud_appointment? If so, what is the next [‘start’] or [‘end’] for?

The end result of the code is to produce an array of appointment data, with the main array index being the URL, and with two sub-array elements ‘start’ and ‘end’ that hold the start and end values.

Most of the comments in the code stating things are arrays are incorrect. Except for the two lines of code inside the foreach(){} loop, building the appointments array, everything is an object.

BTW - if the json_decode() statement sets the second parameter to a TRUE value, the decoded data would already be an array, and you could produce the final array of data just using php array functions array_combine() and array_column(). Also, there’s no need for verbose variable names repeating the name of the data. Everything after a major comment stating what the code is doing is for that comment. Just use general variable names. Only the final name that receives the data needs to be specifically named as to the meaning of the data, and since that is the variable receiving the returned result from this function, you can use simple, short names inside the function code. The following should work -

// get appointment data
$data = json_decode(sendRequestToCloud($token, $appointment_url, array2json(array()), 'GET'), true); // note the true parameter
// index the 'results' element data using the 'url' column
$result = array_combine(array_column($data['results'],'url'), $data['results']);
// return the result to the calling code
return $result;

If the actual data contains other elements, they can be filtered out, if the code using the returned result cannot deal with them.

1 Like

It’s a multi-dimensional array, that is an array within an array.
So the first brackets [] is the array key for the parent array.
The next ones are the key for to sub/child array.
The resulting array would be something like:-

	$cloud_appointments_array = [
		'https://example.com/123' => [
			'start' => '2024-04-02 10:30:00',
			'end' => '2024-04-02 11:00:00'
		],
		'https://example.com/124' => [
			'start' => '2024-04-03 09:30:00',
			'end' => '2024-04-03 10:00:00'
		],
	];

Thank you both. I did feel that the code seemed overly complex, just wasn’t sure if it was my fading memory :joy:

The second set of square brackets being the array within the array is what was confusing me, all the references I looked at for nested arrays did not use that notation.

I will rewrite it and see how I get on.

Thanks again.