Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id...
Transcript of Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id...
![Page 1: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/1.jpg)
Array Tips©
[ ‘hip’, ‘hip’, ‘hip’ ]Benoit Viguier
28/10/2016@b_viguier
![Page 2: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/2.jpg)
It’s about Arrays
#0
![Page 3: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/3.jpg)
Vanilla Php#1
![Page 4: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/4.jpg)
Functional Programming
#2
$ids = array_column( $data, 'id' );
$ids = []; foreach($data as $d){ $ids[] = $d['id']; }
![Page 5: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/5.jpg)
No Anonymous Functions
#3
$offset = 10; $ids = []; foreach($data as $d){ $ids[] = $d['id']+$offset; }
$offset = 10; $ids = array_map( function($d) use($offset){ return $d['id'] + 10; }, $data );
![Page 6: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/6.jpg)
Mind Blown
#Bonus
![Page 7: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/7.jpg)
ADVISORYPARENTAL
EXPLICIT PHP
![Page 8: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/8.jpg)
Filter by category_idSample #1
![Page 9: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/9.jpg)
Filter by category_id
$results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){ $results[] = $d; } }
![Page 10: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/10.jpg)
array_intersect
array_intersect( array $array1, array $array2, array ...$arrayN ): array
![Page 11: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/11.jpg)
Filter by category_id$results = array_intersect_key( $data, array_intersect( array_column($data, 'category_id'), $category_ids ) );
![Page 12: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/12.jpg)
Filter by category_id$results = array_intersect_key( $data, array_intersect( array_column($data, 'category_id'), $category_ids ) );
![Page 13: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/13.jpg)
Filter by category_id$results = array_intersect_key( $data, array_intersect( array_column($data, 'category_id'), $category_ids ) );
![Page 14: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/14.jpg)
Filter by category_id$results = array_intersect_key( $data, array_intersect( array_column($data, 'category_id'), $category_ids ) );
![Page 15: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/15.jpg)
Mixing arraysSample #2
![Page 16: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/16.jpg)
Data$data = [ ['A1', 'A2', 'A3'], ['B1'], [], ['D1', 'D2', 'D3', 'D4'], // ... ]; // Expecting // ['A1','B1','D1','A2','D2','A3','D3','D4'];
![Page 17: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/17.jpg)
Data$data = [ ['A1', 'A2', 'A3'], ['B1'], [], ['D1', 'D2', 'D3', 'D4'], // ... ]; // Expecting // ['A1','B1','D1','A2','D2','A3','D3','D4'];
![Page 18: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/18.jpg)
Mixing arrays$result = []; do { $modified = false; foreach ($data as &$collection) { if (empty($collection)) { $result[] = array_shift($collection); $modified = true; } } } while ($modified);
![Page 19: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/19.jpg)
array_map
array_map ( callable $callback, array $array1, array ...$arrayN ) : array
![Page 20: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/20.jpg)
Mixing arrays$result = array_values( array_filter( array_merge( ...array_map(null, ...$data) ) ) );
![Page 21: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/21.jpg)
Mixing arrays$result = array_values( array_filter( array_merge( ...array_map(null, ...$data) ) ) );
![Page 22: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/22.jpg)
Mixing arrays$result = array_values( array_filter( array_merge( ...array_map(null, ...$data) ) ) );
![Page 23: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/23.jpg)
Mixing arrays$result = array_values( array_filter( array_merge( ...array_map(null, ...$data) ) ) );
![Page 24: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/24.jpg)
Mixing arrays$result = array_values( array_filter( array_merge( ...array_map(null, ...$data) ) ) );
![Page 25: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/25.jpg)
Good or Bad ?¯\_( )_/¯
![Page 26: Array Tipsb-viguier.github.io/downloads/talks/ForumPhp-Array-Tips.pdf · Filter by category_id $results = []; foreach ($data as $d){ if (in_array($d['category_id'], $category_ids)){](https://reader033.fdocuments.us/reader033/viewer/2022060419/5f15fb8aa63ada066e30304e/html5/thumbnails/26.jpg)
Thanks !@b_viguier