PHP code example of diacdg / phparray

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

    

diacdg / phparray example snippets




use Diacdg\TypedArray\ArrayList;
use Diacdg\TypedArray\ArrayMap;

$list = new ArrayList('integer', [11, 22, 33]);
foreach ($list as $value) {
  print $value . "\n";
}
/* output:
    11
    22
    33
 */
 
 
$listOfCallable = new ArrayList('callable', ['gettype', function() {print 'Ok!';}]);
/* output:
Array
(
    [0] => gettype
    [1] => Closure Object (...)
)
*/

$objectsList = new ArrayList(\stdClass::class, [new \stdClass(), new \stdClass()]);
foreach ($objectsList as $key => $object) {
    print $key . ' : ';
    var_dump($object);
}
/* output:
    0 : object(stdClass)#320 (0) {}
    1 : object(stdClass)#347 (0) {}
 */


$appFlags = new ArrayMap('string', 'boolean', ['run-tests' => true, 'coverage' => false]);
$appFlags['create-report'] = true;

print_r((array) $appFlags);
/* output:
Array
(
    [run-tests] => 1
    [coverage] => 
    [create-report] => 1
)
*/

unset($appFlags['coverage']);
print_r((array) $appFlags);
/* output:
Array
(
    [run-tests] => 1
    [create-report] => 1
) 
 */


$list = new ArrayList('integer', [11, 22, 'invalid-value']);
/* output:
InvalidArgumentException : Value must be of type integer but value of type string given.
*/