Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHP Map
Search
Aimeos
September 04, 2020
Programming
1
150
PHP Map
Easy to use and elegant handling for PHP arrays using Map objects
Aimeos
September 04, 2020
Tweet
Share
More Decks by Aimeos
See All by Aimeos
GraphQL vs. JSON:API
aimeos
1
230
Responsive E-Mails
aimeos
0
650
gigacommerce
aimeos
0
3.2k
High performance e-commerce in Laravel
aimeos
0
920
E-Commerce in TYPO3 mit Aimeos (Deutsch)
aimeos
0
460
Aimeos - high performance e-commerce in TYPO3
aimeos
0
310
Aimeos e-commerce components
aimeos
1
210
E-Commerce in TYPO3 - Reloaded
aimeos
0
310
Micro services as architectural concept
aimeos
0
700
Other Decks in Programming
See All in Programming
詳解!defer panic recover のしくみ / Understanding defer, panic, and recover
convto
0
250
スケールする組織の実現に向けた インナーソース育成術 - ISGT2025
teamlab
PRO
1
160
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
410
Ruby×iOSアプリ開発 ~共に歩んだエコシステムの物語~
temoki
0
350
testingを眺める
matumoto
1
140
Rancher と Terraform
fufuhu
2
550
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
dafujii
1
630
Laravel Boost 超入門
fire_arlo
3
220
RDoc meets YARD
okuramasafumi
4
170
ProxyによるWindow間RPC機構の構築
syumai
3
1.2k
HTMLの品質ってなんだっけ? “HTMLクライテリア”の設計と実践
unachang113
4
2.9k
AI Coding Agentのセキュリティリスク:PRの自己承認とメルカリの対策
s3h
0
230
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Docker and Python
trallard
46
3.6k
Gamification - CAS2011
davidbonilla
81
5.4k
GraphQLの誤解/rethinking-graphql
sonatard
72
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
Documentation Writing (for coders)
carmenintech
74
5k
Making Projects Easy
brettharned
117
6.4k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
The Language of Interfaces
destraynor
161
25k
Transcript
PHP Array > Map
Aimeos E-Commerce framework
Why?
$list = [ ['id' => '1', 'value' => 'value1'], ['id'
=> '2', 'value' => 'value2'], null ]; Starting point
$list[] = ['id' => '3', 'value' => 'value3']; unset($list[0]); $list
= array_filter($list); sort($list); $pairs = array_column($list, 'value', 'id'); $value = reset($pairs) ?: null; Current way
Solutions
Laravel collections CakePHP collections Arrayy Underscore.php
(ceased) Others
php-map.org
$value = map( $list ) ->push(['id' => '3', 'value' =>
'value3']) ->remove(0) ->filter() ->sort() ->col('value', 'id') ->first(); Using Map
$map[] = ['id' => '3', 'value' => 'value3']; $value =
$map[0]; count($map); foreach($map as $key => value) {} Still possible
More than array
map() new Map() Map::from() Creating maps
map([1, 2, 3]) map(new Map()) map($iterator) map('abc')
Almost all array methods supported Array methods
concat() collapse() duplicates() flat() countBy()
groupBy() Additional methods except() only() pull() take() skip() ...
each() every() nth() pipe() some()
... Accepting closures
$map->each( function( $val, $key ) { echo $key . ':
' . $val; } ); Closure example
concat() every() find() flat() includes()
join() keys() Like Javascript map() pop() push() reduce() reverse() some() ...
Useful methods map([1,2,3])->join(','); // '1,2,3' Map::split('1,2,3'); // map([1,2,3]) map(['a'=>1,'b'=>2])->toUrl(); //
'a=1&b=2' map(['a'=>1,'b'=>2])->toJson(); // '{"a":1,"b":2}'
Fluent interface $map->push( 'value' ) ->remove( 0 ) ->filter() ->sort();
$e = new \RuntimeException('not found'); map([])->first($e); Find or fail
Map::method( 'strrev', function($sep) { return strrev(join($sep, $this->list)); } ); Map::from(['c',
'b', 'a'])->strrev(' > '); // returns 'a > b > c' Custom methods
jQuery style
class MyClass { private $code; private $status = 0; public
function __construct( $code ) { $this->code = $code; } public function setStatus( int $s ) { $this->stat = $s; return $this; } public function getCode() { return $this->code; } } Example class
$objects = Map::from( [ 'a' => new MyClass( 'x' ),
'b' => new MyClass( 'y' ) ] ); Example Map
$result = $objects ->setStatus(1) ->getCode() ->toArray(); Operations on objects
// status of all object is 1 // $result contains:
['a' => 'x', 'b' => 'y'] Result
?
Aimeos php-map.org Twitter: @aimeos facebook.com/Aimeos