PHP code example of aimeos / map
1. Go to this page and download the library: Download aimeos/map 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/ */
aimeos / map example snippets
$list = [['id' => 'one', 'value' => 'value1'], ['id' => 'two', 'value' => 'value2'], null];
$list[] = ['id' => 'three', 'value' => 'value3']; // add element
unset( $list[0] ); // remove element
$list = array_filter( $list ); // remove empty values
sort( $list ); // sort elements
$pairs = array_column( $list, 'value', 'id' ); // create ['three' => 'value3']
$value = reset( $pairs ) ?: null; // return first value
$list = [['id' => 'one', 'value' => 'value1'], ['id' => 'two', 'value' => 'value2'], null];
$value = map( $list ) // create Map
->push( ['id' => 'three', 'value' => 'value3'] ) // add element
->remove( 0 ) // remove element
->filter() // remove empty values
->sort() // sort elements
->col( 'value', 'id' ) // create ['three' => 'value3']
->first(); // return first value
$map[] = ['id' => 'three', 'value' => 'value3'];
$value = $map[0];
count( $map );
foreach( $map as $key => value );
$map->each( function( $val, $key ) {
echo $key . ': ' . $val;
} );
// MyClass implements setStatus() (returning $this) and getCode() (initialized by constructor)
$map = Map::from( ['a' => new MyClass( 'x' ), 'b' => new MyClass( 'y' )] );
$map->setStatus( 1 )->getCode()->toArray();
['a' => MyClass(), 'b' => MyClass()]
['a' => 'x', 'b' => 'y']
function is_map( $var ) : bool
is_map( new Map() );
// true
is_map( [] );
// false
function map( $elements = [] ) : \Aimeos\Map
// array
map( [] );
// null
map( null );
// scalar
map( 'a' );
// object
map( new \stdClass() );
// map object
map( new Map() );
// iterable object
map( new ArrayObject() );
// closure evaluated lazily
map( function() {
return [];
} );
public function __construct( $elements = [] )
// array
new Map( [] );
// null
new Map( null );
// scalar
new Map( 'a' );
// object
new Map( new \stdClass() );
// map object
new Map( new Map() );
// iterable object
new Map( new ArrayObject() );
// closure evaluated lazily
new Map( function() {
return [];
} );
public function __call( string $name, array $params )
Map::method( 'case', function( $case = CASE_LOWER ) {
return new self( array_change_key_case( $this->items, $case ) );
} );
Map::from( ['a' => 'bar'] )->case( CASE_UPPER );
// ['A' => 'bar']
$item = new MyClass(); // with method setStatus() (returning $this) and getCode() implemented
Map::from( [$item, $item] )->setStatus( 1 )->getCode()->toArray();
public static function __callStatic( string $name, array $params )
Map::method( 'foo', function( $arg1, $arg2 ) {} );
Map::foo( $arg1, $arg2 );
public function after( $value ) : self
Map::from( [0 => 'b', 1 => 'a'] )->after( 'b' );
// [1 => 'a']
Map::from( ['a' => 1, 'b' => 0] )->after( 1 );
// ['b' => 0]
Map::from( [0 => 'b', 1 => 'a'] )->after( 'c' );
// []
Map::from( ['a', 'c', 'b'] )->after( function( $item, $key ) {
return $item >= 'c';
} );
// [2 => 'b']
public function all() : array
Map::from( ['a'] )->all();
// ['a']
public function any( \Closure $callback ) : bool
Map::from( ['a', 'b'] )->any( function( $item, $key ) {
return $item === 'a';
} );
// TRUE
Map::from( ['a', 'b'] )->any( function( $item, $key ) {
return !is_string( $item );
} );
// FALSE
public function arsort( int $options = SORT_REGULAR ) : self
Map::from( ['b' => 0, 'a' => 1] )->arsort();
// ['a' => 1, 'b' => 0]
Map::from( ['a', 'b'] )->arsort();
// ['b', 'a']
Map::from( [0 => 'C', 1 => 'b'] )->arsort();
// [1 => 'b', 0 => 'C']
Map::from( [0 => 'C', 1 => 'b'] )->arsort( SORT_STRING|SORT_FLAG_CASE );
// [0 => 'C', 1 => 'b'] because 'C' -> 'c' and 'c' > 'b'
public function arsorted( int $options = SORT_REGULAR ) : self
Map::from( ['b' => 0, 'a' => 1] )->arsorted();
// ['a' => 1, 'b' => 0]
Map::from( ['a', 'b'] )->arsorted();
// ['b', 'a']
Map::from( [0 => 'C', 1 => 'b'] )->arsorted();
// [1 => 'b', 0 => 'C']
Map::from( [0 => 'C', 1 => 'b'] )->arsorted( SORT_STRING|SORT_FLAG_CASE );
// [0 => 'C', 1 => 'b'] because 'C' -> 'c' and 'c' > 'b'
public function asort( int $options = SORT_REGULAR ) : self
Map::from( ['a' => 1, 'b' => 0] )->asort();
// ['b' => 0, 'a' => 1]
Map::from( [0 => 'b', 1 => 'a'] )->asort();
// [1 => 'a', 0 => 'b']
Map::from( [0 => 'C', 1 => 'b'] )->asort();
// [0 => 'C', 1 => 'b'] because 'C' < 'b'
Map::from( [0 => 'C', 1 => 'b'] )->asort( SORT_STRING|SORT_FLAG_CASE );
// [1 => 'b', 0 => 'C'] because 'C' -> 'c' and 'c' > 'b'
public function asorted( int $options = SORT_REGULAR ) : self
Map::from( ['a' => 1, 'b' => 0] )->asorted();
// ['b' => 0, 'a' => 1]
Map::from( [0 => 'b', 1 => 'a'] )->asorted();
// [1 => 'a', 0 => 'b']
Map::from( [0 => 'C', 1 => 'b'] )->asorted();
// [0 => 'C', 1 => 'b'] because 'C' < 'b'
Map::from( [0 => 'C', 1 => 'b'] )->asorted( SORT_STRING|SORT_FLAG_CASE );
// [1 => 'b', 0 => 'C'] because 'C' -> 'c' and 'c' > 'b'
public function at( int $pos )
Map::from( [1, 3, 5] )->at( 0 );
// 1
Map::from( [1, 3, 5] )->at( 1 );
// 3
Map::from( [1, 3, 5] )->at( -1 );
// 5
Map::from( [1, 3, 5] )->at( 3 );
// NULL
public function avg( $col = null ) : float
Map::from( [1, 3, 5] )->avg();
// 3
Map::from( [1, null, 5] )->avg();
// 3
Map::from( [1, 'sum', 5] )->avg();
// 2
Map::from( [['p' => 30], ['p' => 50], ['p' => 10]] )->avg( 'p' );
// 30
Map::from( [['i' => ['p' => 30]], ['i' => ['p' => 50]]] )->avg( 'i/p' );
// 40
Map::from( [30, 50, 10] )->avg( fn( $val, $key ) => $val < 50 );
// 20
public function before( $value ) : self
Map::from( ['a' => 1, 'b' => 0] )->before( 0 );
// ['a' => 1]
Map::from( [0 => 'b', 1 => 'a'] )->before( 'a' );
// [0 => 'b']
Map::from( [0 => 'b', 1 => 'a'] )->before( 'c' );
// []
Map::from( ['a', 'c', 'b'] )->before( function( $item, $key ) {
return $key >= 1;
} );
// [0 => 'a']
public function bool( $key, $default = false ) : bool
Map::from( ['a' => true] )->bool( 'a' );
// true
Map::from( ['a' => '1'] )->bool( 'a' );
// true (casted to boolean)
Map::from( ['a' => 1.1] )->bool( 'a' );
// true (casted to boolean)
Map::from( ['a' => '10'] )->bool( 'a' );
// true (casted to boolean)
Map::from( ['a' => 'abc'] )->bool( 'a' );
// true (casted to boolean)
Map::from( ['a' => ['b' => ['c' => true]]] )->bool( 'a/b/c' );
// true
Map::from( [] )->bool( 'c', function() { return rand( 1, 2 ); } );
// true (value returned by closure is casted to boolean)
Map::from( [] )->bool( 'a', true );
// true (default value used)
Map::from( [] )->bool( 'a' );
// false
Map::from( ['b' => ''] )->bool( 'b' );
// false (casted to boolean)
Map::from( ['b' => null] )->bool( 'b' );
// false (null is not scalar)
Map::from( ['b' => [true]] )->bool( 'b' );
// false (arrays are not scalar)
Map::from( ['b' => '#resource'] )->bool( 'b' );
// false (resources are not scalar)
Map::from( ['b' => new \stdClass] )->bool( 'b' );
// false (objects are not scalar)
Map::from( [] )->bool( 'c', new \Exception( 'error' ) );
// throws exception
public function call( string $name, array $params = [] ) : self
$item = new MyClass( ['myprop' => 'val'] ); // implements methods get() and toArray()
Map::from( [$item, $item] )->call( 'get', ['myprop'] );
// ['val', 'val']
Map::from( [$item, $item] )->call( 'toArray' );
// [['myprop' => 'val'], ['myprop' => 'val']]
public function cast( string $type = 'string' ) : self
Map::from( [true, 1, 1.0, 'yes'] )->cast();
// ['1', '1', '1.0', 'yes']
Map::from( [true, 1, 1.0, 'yes'] )->cast( 'bool' );
// [true, true, true, true]
Map::from( [true, 1, 1.0, 'yes'] )->cast( 'int' );
// [1, 1, 1, 0]
Map::from( [true, 1, 1.0, 'yes'] )->cast( 'float' );
// [1.0, 1.0, 1.0, 0.0]
Map::from( [new stdClass, new stdClass] )->cast( 'array' );
// [[], []]
Map::from( [[], []] )->cast( 'object' );
// [new stdClass, new stdClass]
public function chunk( int $size, bool $preserve = false ) : self
Map::from( [0, 1, 2, 3, 4] )->chunk( 3 );
// [[0, 1, 2], [3, 4]]
Map::from( ['a' => 0, 'b' => 1, 'c' => 2] )->chunk( 2 );
// [['a' => 0, 'b' => 1], ['c' => 2]]
public function clear() : self
Map::from( [0, 1] )->clear();
// internal : []
public function clone() : self
Map::from( [new \stdClass, new \stdClass] )->clone();
// [new \stdClass, new \stdClass]
public function col( string $valuecol = null, string $indexcol = null ) : self
Map::from( [['id' => 'i1', 'val' => 'v1'], ['id' => 'i2', 'val' => 'v2']] )->col( 'val' );
// ['v1', 'v2']
Map::from( [['id' => 'i1', 'val' => 'v1'], ['id' => 'i2', 'val' => 'v2']] )->col( 'val', 'id' );
// ['i1' => 'v1', 'i2' => 'v2']
Map::from( [['id' => 'i1', 'val' => 'v1'], ['id' => 'i2', 'val' => 'v2']] )->col( null, 'id' );
// ['i1' => ['id' => 'i1', 'val' => 'v1'], 'i2' => ['id' => 'i2', 'val' => 'v2']]
Map::from( [['id' => 'ix', 'val' => 'v1'], ['id' => 'ix', 'val' => 'v2']] )->col( null, 'id' );
// ['ix' => ['id' => 'ix', 'val' => 'v2']]
Map::from( [['foo' => ['bar' => 'one', 'baz' => 'two']]] )->col( 'foo/baz', 'foo/bar' );
// ['one' => 'two']
Map::from( [['foo' => ['bar' => 'one']]] )->col( 'foo/baz', 'foo/bar' );
// ['one' => null]
Map::from( [['foo' => ['baz' => 'two']]] )->col( 'foo/baz', 'foo/bar' );
// ['two']
public function collapse( int $depth = null ) : self
Map::from( [0 => ['a' => 0, 'b' => 1], 1 => ['c' => 2, 'd' => 3]] )->collapse();
// ['a' => 0, 'b' => 1, 'c' => 2, 'd' => 3]
Map::from( [0 => ['a' => 0, 'b' => 1], 1 => ['a' => 2]] )->collapse();
// ['a' => 2, 'b' => 1]
Map::from( [0 => [0 => 0, 1 => 1], 1 => [0 => ['a' => 2, 0 => 3], 1 => 4]] )->collapse();
// [0 => 3, 1 => 4, 'a' => 2]
Map::from( [0 => [0 => 0, 'a' => 1], 1 => [0 => ['b' => 2, 0 => 3], 1 => 4]] )->collapse( 1 );
// [0 => ['b' => 2, 0 => 3], 1 => 4, 'a' => 1]
Map::from( [0 => [0 => 0, 'a' => 1], 1 => Map::from( [0 => ['b' => 2, 0 => 3], 1 => 4] )] )->collapse();
// [0 => 3, 'a' => 1, 'b' => 2, 1 => 4]
public function combine( iterable $values ) : self
Map::from( ['name', 'age'] )->combine( ['Tom', 29] );
// ['name' => 'Tom', 'age' => 29]
public function compare( string $value, bool $case = true ) : bool
Map::from( ['foo', 'bar'] )->compare( 'foo' );
// true
Map::from( ['foo', 'bar'] )->compare( 'Foo', false );
// true (case insensitive)
Map::from( [123, 12.3] )->compare( '12.3' );
// true
Map::from( [false, true] )->compare( '1' );
// true
Map::from( ['foo', 'bar'] )->compare( 'Foo' );
// false (case sensitive)
Map::from( ['foo', 'bar'] )->compare( 'baz' );
// false
Map::from( [new \stdClass(), 'bar'] )->compare( 'foo' );
// false
public function concat( iterable $elements ) : self
Map::from( ['foo'] )->concat( ['bar'] );
// ['foo', 'bar']
Map::from( ['foo'] )->concat( new Map( ['bar' => 'baz'] ) );
// ['foo', 'baz']
public function contains( $key, string $operator = null, $value = null ) : bool
Map::from( ['a', 'b'] )->contains( 'a' );
// true
Map::from( ['a', 'b'] )->contains( ['a', 'c'] );
// true
Map::from( ['a', 'b'] )->contains( function( $item, $key ) {
return $item === 'a'
} );
// true
Map::from( [['type' => 'name']] )->contains( 'type', 'name' );
// true
Map::from( [['type' => 'name']] )->contains( 'type', '!=', 'name' );
// false
public function copy() : self
$m = Map::from( ['foo', 'bar'] );
$m2 = $m->copy();
// internal: ['foo', 'bar'] both two maps
public function count() : int
Map::from( ['foo', 'bar'] )->count();
// 2
public function countBy( callable $callback = null ) : self
Map::from( [1, 'foo', 2, 'foo', 1] )->countBy();
// [1 => 2, 'foo' => 2, 2 => 1]
Map::from( [1.11, 3.33, 3.33, 9.99] )->countBy();
// ['1.11' => 1, '3.33' => 2, '9.99' => 1]
Map::from( ['[email protected] ', '[email protected] ', '[email protected] '] )->countBy( function( $email ) {
return substr( strrchr( $email, '@' ), 1 );
} );
// ['gmail.com' => 2, 'yahoo.com' => 1]
public function dd( callable $callback = null ) : void
Map::from( ['a' => 'foo', 'b' => 'bar'] )->sort()->dd()->first();
/*
Array
(
[0] => bar
[1] => foo
)
*/
public static function delimiter( ?string $char = null ) : string
Map::delimiter( '.' );
// '/'
Map::from( ['foo' => ['bar' => 'baz']] )->get( 'foo.bar' );
// 'baz'
public function diff( iterable $elements, callable $callback = null ) : self
Map::from( ['a' => 'foo', 'b' => 'bar'] )->diff( ['bar'] );
// ['a' => 'foo']
Map::from( [0 => 'a'] )->diff( [0 => 'A'], 'strcasecmp' );
// []
Map::from( ['b' => 'a'] )->diff( ['B' => 'A'], 'strcasecmp' );
// []
Map::from( ['b' => 'a'] )->diff( ['c' => 'A'], function( $valA, $valB ) {
return strtolower( $valA ) <=> strtolower( $valB );
} );
// []
public function diffAssoc( iterable $elements, callable $callback = null ) : self
Map::from( ['a' => 'foo', 'b' => 'bar'] )->diffAssoc( new Map( ['foo', 'b' => 'bar'] ) );
// ['a' => 'foo']
Map::from( [0 => 'a'] )->diffAssoc( [0 => 'A'], 'strcasecmp' );
// []
Map::from( ['b' => 'a'] )->diffAssoc( ['B' => 'A'], 'strcasecmp' );
// []
Map::from( ['b' => 'a'] )->diffAssoc( ['c' => 'A'], function( $valA, $valB ) {
return strtolower( $valA ) <=> strtolower( $valB );
} );
// ['b' => 'a']
public function diffKeys( iterable $elements, callable $callback = null ) : self
Map::from( ['a' => 'foo', 'b' => 'bar'] )->diffKeys( new Map( ['foo', 'b' => 'baz'] ) );
// ['a' => 'foo']
Map::from( [0 => 'a'] )->diffKeys( [0 => 'A'], 'strcasecmp' );
// []
Map::from( ['b' => 'a'] )->diffKeys( ['B' => 'X'], 'strcasecmp' );
// []
Map::from( ['b' => 'a'] )->diffKeys( ['c' => 'a'], function( $keyA, $keyB ) {
return strtolower( $keyA ) <=> strtolower( $keyB );
} );
// ['b' => 'a']
public function dump( callable $callback = null ) : self
Map::from( ['a' => 'foo', 'b' => 'bar'] )->dump()->asort()->dump( 'var_dump' );
/*
Array
(
[a] => foo
[b] => bar
)
array(1) {
["b"]=>
string(3) "bar"
["a"]=>
string(3) "foo"
}
*/
public function duplicates( string $col = null ) : self
Map::from( [1, 2, '1', 3] )->duplicates()
// [2 => '1']
Map::from( [['p' => '1'], ['p' => 1], ['p' => 2]] )->duplicates( 'p' )
// [1 => ['p' => 1]]
Map::from( [['i' => ['p' => '1']], ['i' => ['p' => 1]]] )->duplicates( 'i/p' )
// [1 => ['i' => ['p' => '1']]]
public function each( \Closure $callback ) : self
$result = [];
Map::from( [0 => 'a', 1 => 'b'] )->each( function( $value, $key ) use ( &$result ) {
$result[$key] = strtoupper( $value );
return false;
} );
// $result = [0 => 'A']
public function empty() : bool
Map::from( [] )->empty();
// true
Map::from( ['a'] )->empty();
// false
public function equals( iterable $elements ) : bool
(string) $item1 === (string) $item2
Map::from( ['a'] )->equals( ['a', 'b'] );
// false
Map::from( ['a', 'b'] )->equals( ['b'] );
// false
Map::from( ['a', 'b'] )->equals( ['b', 'a'] );
// true
public function every( \Closure $callback ) : bool
Map::from( [0 => 'a', 1 => 'b'] )->every( function( $value, $key ) {
return is_string( $value );
} );
// true
Map::from( [0 => 'a', 1 => 100] )->every( function( $value, $key ) {
return is_string( $value );
} );
// false
public function except( $keys ) : self
Map::from( ['a' => 1, 'b' => 2, 'c' => 3] )->except( 'b' );
// ['a' => 1, 'c' => 3]
Map::from( [1 => 'a', 2 => 'b', 3 => 'c'] )->except( [1, 3] );
// [2 => 'b']
public static function explode( string $delimiter , string $string , int $limit = PHP_INT_MAX ) : self
Map::explode( ',', 'a,b,c' );
// ['a', 'b', 'c']
Map::explode( '<-->', 'a a<-->b b<-->c c' );
// ['a a', 'b b', 'c c']
Map::explode( '', 'string' );
// ['s', 't', 'r', 'i', 'n', 'g']
Map::explode( '|', 'a|b|c', 2 );
// ['a', 'b|c']
Map::explode( '', 'string', 2 );
// ['s', 't', 'ring']
Map::explode( '|', 'a|b|c|d', -2 );
// ['a', 'b']
Map::explode( '', 'string', -3 );
// ['s', 't', 'r']
public static function fill( int $num, $value, int $start = 0 ) : self
Map::fill( 3, 'a' );
// [0 => 'a', 1 => 'a', 2 => 'a']
Map::fill( 3, 'a', 2 );
// [2 => 'a', 3 => 'a', 4 => 'a']
Map::fill( 3, 'a', -2 );
// [-2 => 'a', -1 => 'a', 0 => 'a'] (PHP 8)
// [-2 => 'a', 0 => 'a', 1 => 'a'] (PHP 7)
php
public function filter( callable $callback = null ) : self
php
(bool) $value === false
php
Map::from( [null, 0, 1, '', '0', 'a'] )->filter();
// [1, 'a']
Map::from( [2 => 'a', 6 => 'b', 13 => 'm', 30 => 'z'] )->filter( function( $value, $key ) {
return $key < 10 && $value < 'n';
} );
// ['a', 'b']
php
public function find( \Closure $callback, $default = null, bool $reverse = false )
php
public function findKey( \Closure $callback, $default = null, bool $reverse = false )
php
public function first( $default = null )
php
public function firstKey()
php
public function flat( int $depth = null ) : self
php
public function flip() : self
php
public function float( $key, $default = 0.0 ) : float
php
public static function from( $elements = [] ) : self
php
public static function fromJson( string $json, int $options = JSON_BIGINT_AS_STRING ) : self
php
JSON_BIGINT_AS_STRING|JSON_INVALID_UTF8_IGNORE
php
public function get( $key, $default = null )
php
public function getIterator() : \ArrayIterator
php
foreach( Map::from( ['a', 'b'] ) as $value ) {
// ...
}
php
public function grep( string $pattern, int $flags = 0 ) : self
php
public function groupBy( $key ) : self
php
public function has( $key ) : bool
php
public function if( $condition, \Closure $then, \Closure $else = null ) : self
php
Map::from( ['a' => 1, 'b' => 0] )->if(
'a' == 'b',
function( Map $_ ) { echo "then"; }
);
// no output
Map::from( ['a' => 1, 'b' => 0] )->if(
function( Map $map ) { return $map->has( 'a' ); },
function( Map $_ ) { echo "then"; },
function( Map $_ ) { echo "else"; }
);
// then
Map::from( ['a' => 1, 'b' => 0] )->if(
fn( Map $map ) => $map->has( 'c' ),
function( Map $_ ) { echo "then"; },
function( Map $_ ) { echo "else"; }
);
// else
Map::from( ['a', 'b'] )->if( true, function( $map ) {
return $map->push( 'c' );
} );
// ['a', 'b', 'c']
Map::from( ['a', 'b'] )->if( false, null, function( $map ) {
return $map->pop();
} );
// ['b']
php
public function ifAny( \Closure $then = null, \Closure $else = null ) : self
php
public function ifEmpty( \Closure $then = null, \Closure $else = null ) : self
php
public function index( $value ) : ?int
php
public function insertAfter( $element, $value ) : self
php
public function insertAt( int $pos, $element, $key = null ) : self
php
public function insertBefore( $element, $value ) : self
php
public function inString( $value, bool $case = true ) : bool
php
public function int( $key, $default = 0 ) : int
php
public function intersect( iterable $elements, callable $callback = null ) : self
php
public function intersectAssoc( iterable $elements, callable $callback = null ) : self
php
public function intersectKeys( iterable $elements, callable $callback = null ) : self
php
public function isEmpty() : bool
php
public function isList() : bool
php
public function isObject() : bool
php
public function isNumeric() : bool
php
public function isScalar() : bool
php
public function isString() : bool
php
public function join( $glue = '' ) : string
php
public function jsonSerialize()
php
public function keys() : self
php
public function krsort( int $options = SORT_REGULAR ) : self
php
public function krsorted( int $options = SORT_REGULAR ) : self
php
public function krsorted( int $options = SORT_REGULAR ) : self
php
public function ksort( int $options = SORT_REGULAR ) : self
php
public function ksorted( int $options = SORT_REGULAR ) : self
php
public function last( $default = null )
php
public function lastKey()