PHP code example of timdev / array-undot

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

    

timdev / array-undot example snippets


// myapp.local.php
return [
    // sometimes it's nicer (reading and writing) to do this:
    'path.to.some.key' => 'value',

    // as opposed to this:
    'path' => [
        'to' => [
            'some' => [
                'key' => 'value'
            ]
        ]
  ]
];

use \TimDev\ArrayUndot\Undotter;

/* Example 1: The basic idea: */ 
Undotter::undot(['a.b' => 'FOO']); 
// => ['a' => ['b' => 'FOO']]

/* Example 2: Dotted keys don't need to be at the top level: */
Undotter::undot(['top' => ['a' => ['b.c' => 'FOO']]]);
// => ['top' => ['a' => ['b' => ['c' => 'FOO']]]]

/* Example 3: Conflict-Resolution depends on the order of elements in the input: */ 
Undotter::undot([
    'a' => ['b' => 'FOO'],
    'a.b' => 'BAR'
]); 
// => ['a' => ['b' => 'BAR']]

// ... but if the order is swapped
Undotter::undot([
    'a.b' => 'BAR',
    'a' => ['b' => 'FOO']
]); 
// => ['a' => ['b' => 'FOO']]

/* Example 4:  If the conflicting values are both arrays, they're merged (using the same logic as ConfigAggregator): */
Undotter::undot([
    'a' => ['b' => ['foo' => 1, 'bar' => 2, 'baz' => 3]],
    'a.b' => ['baz' => 4, 'qux' => 5]
]);
// => 
//    [
//        'a' => [
//            'b' => [
//                'foo' => 1, 
//                'bar', => 2, 
//                'baz' => 4, 
//                'qux' => 5
//            ]
//        ]
//    ]