Overview
TheObjectify class provides utility methods for converting JSON strings to PHP arrays. It includes support for recursive mapping and handling arrays of JSON strings.
Class Reference
Methods
json_mapper()
Decode JSON strings to arrays with optional recursive processing.mixed
required
The value to process. Can be:
- JSON string: Will be decoded to array
- Array: Will recursively process each element (if
$recursiveis true) - Other: Returns unchanged
bool
default:"true"
Whether to recursively process array elements
mixed
- Decoded array if input is valid JSON string
- Recursively processed array if input is array and
$recursiveis true - Original value if not JSON or array
Example Usage
json_mapper_norecurse()
Decode JSON strings without recursive processing (alias forjson_mapper() with recursive = false).
mixed
required
The value to process
mixed
Decoded value without recursive array processing
Example Usage
json_to_array()
Convert JSON strings in an array to decoded arrays with optional recursive processing.mixed
required
Array to process. If not an array, it will be converted to a single-element array
bool
default:"true"
Whether to recursively process nested elements
array
Array with JSON strings decoded to arrays
Example Usage
Use Cases
Processing API Responses
Batch Processing JSON Data
Handling Nested JSON
Complete Example
Notes
The
json_mapper() method will return the original value unchanged if:- The value is not a string
- The string is not valid JSON
- JSON decoding fails