PHP code example of nzo / array

1. Go to this page and download the library: Download nzo/array 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/ */

    

nzo / array example snippets


// Set nested array value
$array = Arr::set([], 'key1.key2.key3', 'my_value'); 
// Which is equivalent to
[
  'key1' => [
    'key2' => [
      'key3' => 'my_value'
    ]
  ]
]

// Get nested array value
Arr::get($array, 'key1.key2') -> ['key3' => 'my_value']

// Check if array has nested element
Arr::has($array, 'key1.key2.key3') -> true 

// Map array while accessing it's key
Arr::map($array, function ($key, $value) {
   // Your code here
});

// Find array element
Arr::find($array, function ($element) {
  return Arr::get($element, 'key2.key3') === 'my_value';
}) -> [ 'key2' => [ 'key3' => 'my_value'] ]

// Chain few methods
Arr::obj(['test' => 1, 'foo' => 'bar'])
    ->set('abc', 123)
    ->set('[]', 'auto_index')
    ->remove('foo')
    ->getArray() 
->
[
  'test' => 1,
  'abc' => 123,
  'auto_index'
]

// Group objects by the result of calling method 'getSize' on each object
Arr::groupObjects([$cat, $dog, $fish, ...], 'getSize') ->
[
  'medium' => [$cat, $dog, ...],
  'small' => [$fish, ...],
  ...
]