PHP code example of oliverde8 / associative-array-simplified

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

    

oliverde8 / associative-array-simplified example snippets


$array = ['a' => ['b' => 2, 'c' => 3]];
if (isset($array['a'])) {
    $valB = isset($array['a']['b']) ? $array['a']['b'] : "default";
    $valB = isset($array['a']['c']) ? $array['a']['c'] : "default";
}

$object = new AssociativeArray( ['a' => ['b' => 2, 'c' => 3]]);
$valB = $object->get('a/b', 'default');
$valB = $object->get('a/c', 'default');

$array = ['a' => 1, 'b' => 2,];
$object = new AssociativeArray($array);

$object->get('a');
//This will return '1'

$object->get('c');
//This will return null

$object->get('c', 10);
//This will return 10

$array = ['a' => ['b' => 2]];
$object = new AssociativeArray($array);

$object->get('a');
//This will return ['b' => 2]

$object->get(['a', 'b']);
//This will return 2

$object->get('a/b');
//This will return 2

// Both of these do the same thing.
$object->set('a/d', 10);
$object->set(['a','d'], 10);

// And you get back the array.
$object->array()

$array = ['a' => ['b' => 2]];
$object = new AssociativeArray($array);
$this->get('a/b');

$array = ['a' => ['b' => 2]];
$object = new AssociativeArray($array, '.');
$this->get('a.b');

$array = ['a' => ['b' => 2]];
AssociativeArray::getFromKey($array, 'a/b'); 
// Will return 2

$array = ['a' => ['b' => 2]];
AssociativeArray::setFromKey($array, 'a/c', 'yop); 
// Will return ['a' => ['b' => 2, 'c' => 'yop']] 

$array = ['a' => ['b' => 2]];
$object = new AssociativeArray($array);

$object->get('a');
//This will return ['b' => 2]

$object->get(a)->get('b');
//Should return 2

$object->get(a)->get('b')->get('c', 'default');
//Will make an error but should have returned 'default'