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