News

Handle Nested Data Structures in PHP with the Data Block Package

Published
Handle Nested Data Structures in PHP with the Data Block Package image

The Data Block package for PHP can handle querying, filtering, and setting nested data structures. This package offers a streamlined approach to handling nested data and complex JSON structures. You can create a Block object in multiple ways, such as a JSON file, an array, or a URL, for example:

use HiFolks\DataType\Block;
use HiFolks\DataType\Enums\Operator;

Block::fromJsonUrl('https://api.github.com/orgs/hi-folks/repos')
    ->select('full_name', 'stargazers_count')
    ->where('stargazers_count', Operator::GREATER_THAN, 0)
    ->orderBy('stargazers_count', 'desc')
    ->forEach(
        function ($item) {
            echo $item->get('full_name').' : ';
            echo $item->get('stargazers_count').PHP_EOL;
        }
    );

Apart from the many methods to conditionally get data, filter and order data, you can also get and set nested data, extract keys, and many other operations you would expect from working with complex data:

use HiFolks\DataType\Block;

$fruits = [
    "avocado" =>
    [
        'name' => 'Avocado',
        'fruit' => '🥑',
        'wikipedia' => 'https://en.wikipedia.org/wiki/Avocado',
        'color'=>'green',
        'rating' => 8
    ],
    // ...
];

$data = Block::make($fruitsArray);

$data->get('avocado'); // returns an array
$data->get('avocado.color'); // returns the string "green"

$data->set('cherry', [
    'name' => 'Cherry',
    'fruit' => '🍒',
    'wikipedia' => 'https://en.wikipedia.org/wiki/Cherry',
    'color' => 'red',
    'rating' => 9,
]);

$data->set('cherry.rating', 5);

Besides working with data using the Block object, you can also export it to various formats such as a JSON string, JSON file, or YAML. This package is documented well in the GitHub README file.

🧑‍💻 You can get started with this package on GitHub: Hi-Folks/data-block

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article