"php-array" Problem with Character Equipment API


I´m using the “Character Equipment API”. I decode the json data in a php array.

The problem now is that the entry-numbers of the arrays are changing, depending if the character has empty item-slots.

Here is an example:

Character 1 has full equipment. The array for the back has the following access data:

Character 2 is missing some quipment in the armory (like shirt, weapon etc.). Now the array access data for the back would be:


So it changed the access-number since there is no array for a free item-slot. This makes it impossible to echo out the right item-slot for a character.

Could you please help me how to handle that?

Nothing weird about that, many APIs return arrays of variable lengths. In this case you must always use the slot field value to determine the correct item type.

One simple approach to make it more readable for later use is to create a new named array using the slot property as the key:

$raw_json_string = consume_api(); 
$data = json_decode($raw_json_string);

$named_slots = [];
foreach ($data->equipped_items as $item) {
  $named_slots[$item->slot->type] = $item;

Later on you can simply use the slot you want:

$shoulder = $named_items['SHOULDER'];

Note: I don’t have any PHP environment set up, there might be some errors on the code above, but it should serve as a starting point.


Thank you. That´s it.