PHP code example of danilopolani / laravel-array-destructuring

1. Go to this page and download the library: Download danilopolani/laravel-array-destructuring library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

danilopolani / laravel-array-destructuring example snippets


$post = [
	'title' => 'Article 1',
	'slug' => 'article-1',
	'description' => 'Lorem ipsum',
	'tags' => ['foo', 'bar'],
	'gallery' => [
	    ['image' => 'image.jpg'],
	    ['image' => 'image2.jpg'],
	],
];

[$tags, $article] = Arr::destructure($post, 'tags');

dump($tags); // ['foo', 'bar']
dump($article) // ['title' => 'Article 1', 'slug' => 'article-1', ...] without tags

[$notFoundKey, $article] = Arr::destructure($post, 'notFoundKey');

dump($notFoundKey); // null

$post = [
	'title' => 'Article 1',
	'slug' => 'article-1',
	'description' => 'Lorem ipsum',
	'tags' => ['foo', 'bar'],
	'gallery' => [
	    ['image' => 'image.jpg'],
	    ['image' => 'image2.jpg'],
	],
];

[$tags, $gallery, $article] = Arr::destructure($post, ['tags', 'gallery']);

dump($tags); // ['foo', 'bar']
dump($gallery); // [['image' => 'image.jpg'], ['image' => 'image2.jpg']]
dump($article) // ['title' => 'Article 1', 'slug' => 'article-1', 'description' => 'Lorem ipsum']

$post = [
	'title' => 'Article 1',
	'slug' => 'article-1',
	'description' => 'Lorem ipsum',
	'tags' => ['foo', 'bar'],
	'gallery' => [
	    ['image' => 'image.jpg'],
	    ['image' => 'image2.jpg'],
	],
];

[$slug, $meta, $article] = Arr::destructure($post, ['slug', ['tags', 'gallery']]);

dump($slug); // article-1
dump($meta); // ['tags' => ['foo', 'bar'], 'gallery' => ['image' => 'image.jpg'], ['image' => 'image2.jpg']]
dump($article) // ['title' => 'Article 1', 'description' => 'Lorem ipsum']

[$notFoundGroup, $article] = Arr::destructure($post, [['notFound1', 'notFound2']]);

dump($notFoundGroup); // []