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
120
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
200
Responsive E-Mails
aimeos
0
640
gigacommerce
aimeos
0
3.1k
High performance e-commerce in Laravel
aimeos
0
910
E-Commerce in TYPO3 mit Aimeos (Deutsch)
aimeos
0
440
Aimeos - high performance e-commerce in TYPO3
aimeos
0
300
Aimeos e-commerce components
aimeos
1
210
E-Commerce in TYPO3 - Reloaded
aimeos
0
300
Micro services as architectural concept
aimeos
0
680
Other Decks in Programming
See All in Programming
.NET Aspireのクラウド対応検証: Azureと他環境での実践
ymd65536
1
670
rtcamp 10 (vk-illuminati)
yumcyawiz
1
210
Subclassing, Composition, Python, and You
hynek
3
190
Why I Choose NetBeans for Jakarta EE
ivargrimstad
0
850
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
arthur1
0
440
Piniaの現状と今後
waka292
2
520
個人開発で使ってるやつを紹介する回
yohfee
1
730
tsconfig.jsonの最近の新機能 ファイルパス編
uhyo
7
1.9k
tc39 x jsconf.jp Panel Discussion 2024
yosuke_furukawa
PRO
0
110
Hi, have you met Kotlin Multiplatform? | DevFest Vienna 2024
prof18
0
230
現場から考えるソフトウェアエンジニアリングの価値と実験
nomuson
1
140
モジュラモノリス、その前に / Modular monolith, before that
euglena1215
8
750
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
131
8.9k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
13
1.8k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Scaling GitHub
holman
458
140k
Testing 201, or: Great Expectations
jmmastey
38
7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
664
120k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
4 Signs Your Business is Dying
shpigford
180
21k
Code Review Best Practice
trishagee
64
17k
Adopting Sorbet at Scale
ufuk
73
9k
Optimising Largest Contentful Paint
csswizardry
31
2.9k
A Tale of Four Properties
chriscoyier
156
22k
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