numbered_props_to_array

Separate an object with numbered properties into an array of objects

Definition

array[object] numbered_props_to_array(object $object)

Takes an object with a specific structure, where properties are indexed with an integer suffix, and splits it into an array of objects where the properties with the same suffix are grouped.

Parameters

object $object

An object whose properties are numbered with a suffix.

Returns

array[object]

An array of objects. The elements of the array are all the properties that had the same suffix in the input data. Suffixes are removed in the resulting array.

Example

{
  "info": {
    "name": "John",
    "location": "Gainesville, Florida, USA",
    "name2": "Jean",
    "name3": "Laura",
    "location3": "London, UK",
    "location4": "Madrid, ESP",
    "location2": "Lyon, FR",
    "name6": "Maria"
  }
}
{
  people: numbered_props_to_array(info)
}
{
  "people": [
    {"name": "John", "location": "Gainesville, Florida, USA"},
    {"name": "Jean", "location": "Lyon, FR"},
    {"name": "Laura", "location": "London, UK"},
    {"location": "Madrid, ESP"},
    {"name": "Maria"}
  ]
}

Remarks

This function can be useful for a specific type of unprocessed data. The outputs from different systems can often be structured in a way that's difficult to analyze, and this function helps us organize one of the common structures that would be difficult to use otherwise.