PHP code example of pavlyuts / php-string-key-arrays

1. Go to this page and download the library: Download pavlyuts/php-string-key-arrays 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/ */

    

pavlyuts / php-string-key-arrays example snippets




use SKArray\SKArray;
use SKArray\SKArrayException;

$arr =  new SKArray();

$arr['StringKey'] = 'Value1';

// In standart array this key becomes int(123), but here it be kept string 
$arr['123'] = 'ValueString123';

// This will REWRITE last one becuse int key 123 becomes string key '123'!
$arr[123] = 'ValueInt123';

//this will work
foreach ($arr as $key => $val) {
    echo "$key: $val\n";
}



use SKArray\SKArray;
use SKArray\SKArrayException;

$arr =  new SKArray();

//This will work fine
$var = $arr['UnknownnKey'] ?? null;

//This will throw exception
try {
    var = $arr['Unknownn'];
} catch (SKArrayException $ex) {
    echo "Check your key!\n";
}

//This will not throw, only PHP notice and null return
$arr2 = new SKArray(false);
$var = $arr2['Unknow'];


//This will throw `SKArrayException` as the key is not known
$a = new SKArray();
$a['Level1']['Level2'] = 'data';

//This will NOT throw exception, but also it will not change contained array element
$a['Level1'] = [];
$a['Level1']['Level2'] = 'data';
// Result: $a['Level1'] == [], element not modified, PHP notice generated

public function column($column, ...$args): SKArray

public function setSubarrayItem($offset, $value, $key = null)