PHP code example of binary-cube / dot-array

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

    

binary-cube / dot-array example snippets


DotArray::create(['config' => ['some.dotted.key' => 'value']])->get('config.{some.dotted.key}')

        new DotArray($array);
        DotArray::create($array);
        DotArray::createFromJson($jsonString);
        

        // Because the key `sci-fi & fantasy` is array the returning value it will be a new instance of DotArray.
        $dot('books.{sci-fi & fantasy}');
        
        // Because the price is not an array, the result will be raw data, float in this case.
        $dot('books.{sci-fi & fantasy}.0.price');
        
        // Accessing the raw array.
        $dot('books.{sci-fi & fantasy}')->toArray();
        $dot->get('books.{sci-fi & fantasy}')->toArray();
        
        // Accessing the last leaf and getting the raw data.
        $dot('books.{sci-fi & fantasy}.0.name');
        $dot->get('books.{sci-fi & fantasy}.0.name');

        // Giving a default value in case the requested key is not found.
        $dot->get('key.not.exist', 'not-found-as-string');
        
        // Vanilla PHP.
        $dot('books.{sci-fi & fantasy}.0.name');
        $dot['books']['sci-fi & fantasy'][0]['name'];
        

        // Using dotted key and accessing without getting confused.
        // Allowed tokens for keeping the names with dot(.) togethers are: '', "", [], (), {}
        $dot->get('config.{elastic-search}.\'v5.0\'.host')
        $dot->get('config.{elastic-search}."v5.0".host')
        $dot->get('config.{elastic-search}.[v5.0].host')
        $dot->get('config.{elastic-search}.(v5.0).host')
        $dot->get('config.{elastic-search}.{v5.0}.host')
        

        $dot->set('books.{sci-fi & fantasy}.0.name', 'New Name');
        
        // Vanilla PHP.
        $dot['books.{sci-fi & fantasy}.0.name'] = 'New Name';
        $dot['books']['sci-fi & fantasy'][0]['name'] = 'New Name';
        

        $dot->clear('books.{sci-fi & fantasy}');
        $dot->clear('books.{sci-fi & fantasy}', null);
        $dot->clear('books.{sci-fi & fantasy}.0.name', null);
        
        // Multiple keys.
        $dot->clear([
          'books.{sci-fi & fantasy}',
          'books.{children\'s books}'
        ]);
        
        // Vanilla PHP.
        $dot['books.{sci-fi & fantasy}'] = [];
        

        $dot->delete('books.{sci-fi & fantasy}');
        $dot->delete('books.{sci-fi & fantasy}.0.name');
        $dot->delete(['books.{sci-fi & fantasy}.0', 'books.{children\'s books}.0']);
        

        // Example 1.
        $dot->merge(['key_1' => ['some_key' => 'some_value']]);

        // Example 2.
        $dot->merge(
            [
                'key_1' => ['some_key' => 'some_value'],
            ], 
            [
                'key_2' => ['some_key' => 'some_value'],
            ],
            [
                'key_n' => ['some_key' => 'some_value']
            ],
        );
        

        $book = $dot->get('books.{children\'s books}')->find(function ($value, $key) {
           return $value['price'] > 0;
        });
        

        $books = $dot->get('books.{children\'s books}')->filter(function ($value, $key) {
            return $value['name'] === 'Harry Potter and the Order of the Phoenix';
        });
        
        $books->toArray();
        

        /*
            Allowed comparison operators:
                - [ =, ==, eq (equal) ]
                - [ ===, i (identical) ]
                - [ !=, ne (not equal) ]
                - [ !==, ni (not identical) ]
                - [ <, lt (less than) ]
                - [ >, gr (greater than) ]
                - [ <=, lte (less than or equal to) ]
                - [ =>, gte (greater than or equal to) ]
                - [ in, contains ]
                - [ not-in, not-contains ]
                - [ between ]
                - [ not-between ]
        */
        // Example 1.
        $books = $dot->get('books.{children\'s books}')->filterBy('price', 'between', 5, 12);

        // Example 2.
        $books = $dot->get('books.{children\'s books}')->filterBy('price', '>', 10);

        // Example 3.
        $books = $dot->get('books.{children\'s books}')->filterBy('price', 'in', [8.5, 15.49]);
        

        /*
            The signature of the `where` call can be:
                - where([property, comparisonOperator, ...value])
                - where(\Closure) :: The signature of the callable must be: `function ($value, $key)`

            Allowed comparison operators:
                - [ =, ==, eq (equal) ]
                - [ ===, i (identical) ]
                - [ !=, ne (not equal) ]
                - [ !==, ni (not identical) ]
                - [ <, lt (less than) ]
                - [ >, gr (greater than) ]
                - [ <=, lte (less than or equal to) ]
                - [ =>, gte (greater than or equal to) ]
                - [ in, contains ]
                - [ not-in, not-contains ]
                - [ between ]
                - [ not-between ]
        */
        
        // Example 1. (using the signature: [property, comparisonOperator, ...value])
        $books = $dot->get('books.{children\'s books}')->where(['price', 'between', 5, 12]);

        // Example 2. (using the signature: [property, comparisonOperator, ...value])
        $books = $dot->get('books.{children\'s books}')->where(['price', '>', 10]);
        
        // Example 3. (using the signature: [property, comparisonOperator, ...value])
        $books = $dot->get('books.{children\'s books}')->where(['price', 'in', [8.5, 15.49]]);
        
        // Example 4. (using the signature: \Closure)
        $books = $dot->get('books.{children\'s books}')->where(function ($value, $key) {
            return $value['name'] === 'Harry Potter and the Order of the Phoenix';
        });
        

        // Example 1.
        $dot->toArray();

        // Example 2.
        $dot->get('books.{sci-fi & fantasy}')->toArray();
        

        // Example 1.
        $dot->toJson();

        // Example 2.
        $dot->get('books.{sci-fi & fantasy}')->toJson();
        

        $dot = DotArray::create(
            [
                'a' => [
                    'b' => 'value',
                ],
    
                'b' => [
                    1,
                    2,
                    3,
                    'array' => [
                        1,
                        2,
                        3,
                    ]
                ],
            ]
        );

        $dot->toFlat();

        /*
            The output will be an array:
            [
                '{a}.{b}' => 'value',
                '{b}.{0}' => 1,
                '{b}.{1}' => 2,
                '{b}.{2}' => 3,
                '{b}.{array}.{0}' => 1,
                '{b}.{array}.{1}' => 2,
                '{b}.{array}.{2}' => 3,
            ],
        */
        

$dummyArray = [
    'books' => [
        'sci-fi & fantasy' => 
            [
                [
                    'name'      => 'Chronicles of Narnia Box Set',
                    'price'     => 24.55,
                    'currency'  => '$',
                    'authors'   => 
                        [
                            [
                                'name' => 'C.S. Lewis'
                            ],
                        ],
                ],
                [
                    'name'      => 'A Game of Thrones / A Clash of Kings / A Storm of Swords / A Feast of Crows / A Dance with Dragons ',
                    'price'     => 37.97,
                    'currency'  => '$',
                    'authors'   => 
                        [
                            [
                                'name' => 'George R. R. Martin'
                            ],
                         ],
                ],
            ],
        
        'children\'s books' => 
            [
                [
                    'name'      => 'Harry Potter and the Order of the Phoenix',
                    'price'     => 15.49,
                    'currency'  => '$',
                    'authors'   => 
                        [
                            [
                                'name' => 'J. K. Rowling'
                            ],
                        ],
                ],
                [
                    'name'      => 'Harry Potter and the Cursed Child',
                    'price'     => 8.5,
                    'currency'  => '$',
                    'authors'   => 
                        [
                            [
                                'name' => 'J. K. Rowling',
                            ],
                            [
                                'name' => 'Jack Thorne'
                            ],
                        ],
                ],
            ],
        
    ],
];