numbered_props_to_array

Modified on Thu, 9 Jul at 10:50 AM

numbered_props_to_array

TABLE OF CONTENTS

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

Sample data
{
  "info": {
    "name": "John",
    "location": "Gainesville, Florida, USA",
    "name2": "Jean",
    "name3": "Laura",
    "location3": "London, UK",
    "location4": "Madrid, ESP",
    "location2": "Lyon, FR",
    "name6": "Maria"
  }
}
Transformation
{
  people: numbered_props_to_array(info)
}
Result
{
  "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.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article