Upload
michelangelo-van-dam
View
618
Download
8
Embed Size (px)
Citation preview
Zend Framework2How arrays will save your project
in it2PROFESSIONAL PHP SERVICES
Michelangelo van DamPHP Consultant, Community Leader & Trainer
http
s://w
ww.
flick
r.com
/pho
tos/
akra
bat/8
7843
1881
3
ScheduleIntroduction to ZF2
The array
Challenges in development
Solutions offered in ZF2
More options
ScheduleIntroduction to ZF2
The array
Challenges in development
Solutions offered in ZF2
More options
“The Array Framework”
<?php /** * Zend Framework 2 Configuration Settings * */ return array( 'modules' => array( 'Application', 'In2it\\SsoProvider', 'In2it\\Crm\\Dashboard', 'In2it\\Crm\\ContactManager', 'In2it\\Crm\\OrderManager', 'In2it\\Crm\\ProductManager', ), 'module_listener_options' => array( 'module_paths' => array( './module', './vendor' ), 'config_glob_paths' => array( '/home/dragonbe/workspace/Totem/config/autoload/{,*.}{global,local}.php' ), 'config_cache_key' => 'application.config.cache', 'config_cache_enabled' => true, 'module_map_cache_key' => 'application.module.cache', 'module_map_cache_enabled' => true, 'cache_dir' => 'data/cache/' ) );
<?phpnamespace In2it\Crm\Dashboard;
use Zend\ModuleManager\Feature\ConfigProviderInterface; use Zend\Mvc\ModuleRouteListener; use Zend\Mvc\MvcEvent;
class Module implements ConfigProviderInterface { public function getAutoloaderConfig() { return array( 'Zend\Loader\ClassMapAutoloader' => array( __DIR__ . '/autoload_classmap.php', ), 'Zend\Loader\StandardAutoloader' => array( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), ), ); }
public function getConfig() { return include __DIR__ . '/config/module.config.php'; } }
http
s://w
ww.
flick
r.com
/pho
tos/
dasp
rid/8
1479
8630
7
Yaml
XML
INI
CSV PHP
PHP
ScheduleIntroduction to ZF2
The array
Challenges in development
Solutions offered in ZF2
More options
array_change_key_case array_chunk array_column
array_combine array_count_values
array_diff_assoc array_diff_key
array_diff_uassoc array_diff_ukey
array_diff array_fill_keys
array_fill array_filter array_flip
array_intersect_assoc array_intersect_key
array_intersect_uassoc array_intersect_ukey
array_intersect array_key_exists
array_keys array_map
array_merge_recursive array_merge
array_multisort array_pad array_pop
array_product
array_push array_rand
array_reduce array_replace_recursive
array_replace array_reverse array_search
array_shift array_slice
array_splice array_sum
array_udiff_assoc array_udiff_uassoc
array_udiff array_uintersect_assoc array_uintersect_uassoc
array_uintersect array_unique array_unshift array_values
array_walk_recursive array_walk
array arsort asort
compact count
current
each end
extract in_array
key_exists key
krsort ksort list
natcasesort natsort
next pos prev
range reset rsort
shuffle sizeof sort
uasort uksort usort
Do you know them?
array_search array_filter
? ? ? ??
? ? ??
??
???
? ? ?
count array_sum
array_product
? ? ? ??
? ? ??
??
???
? ? ?
array_diff array_intersect array_merge
? ? ? ??
? ? ??
??
???
? ? ?
// Let's take one value out of our array $array = ['apple', 'banana', 'chocolate'];
$newArray = []; foreach ($array as $value) { if ('banana' !== $value) { $newArray[] = $value; } } echo implode(', ', $newArray);
// Outputs: apple, chocolate
<?php
// Let's take one value out of our array $array = ['apple', 'banana', 'chocolate'];
// I keep an ignore list as well $ignore = ['banana'];
// Ready for magic? echo implode(', ', array_diff($array, $ignore));
// Outputs: apple, chocolate
<?php
// I have one associative array $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'chocolate'];
// And a similar value array $similar = ['banana', 'chocolate'];
// I need to get the keys of similar items from original array $newSimilar = []; foreach ($array as $key => $value) { if (in_array($value, $similar)) { $newSimilar[$key] = $value; } } $similar = $newSimilar; unset ($newSimilar);
var_dump($similar);
/* Outputs: array(2) { 'b' => string(6) "banana" 'c' => string(9) "chocolate" } */
<?php
// I have one associative array $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'chocolate'];
// And a similar value array $similar = ['banana', 'chocolate'];
// I need to get the keys of similar items from original array $similar = array_intersect($array, $similar);
var_dump($similar);
/* Outputs: array(2) { 'b' => string(6) "banana" 'c' => string(9) "chocolate" } */
One more?
<?php
// I have one associative array $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'chocolate'];
// I need to find a given value -> 'chocolate' $search = 'chocolate'; $searchResult = []; foreach ($array as $key => $value) { if ($search === $value) { $searchResult[$key] = $value; } } var_dump($searchResult);
/* Outputs: array(1) { 'c' => string(9) "chocolate" } */
<?php
// I have one associative array $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'chocolate'];
// I need to find a given value -> 'chocolate' $search = 'chocolate'; $searchResult = array_filter($array, function ($var) use ($search) { return $search === $var; }); var_dump($searchResult);
/* Outputs: array(1) { 'c' => string(9) "chocolate" } */
ScheduleIntroduction to ZF2
The array
Challenges in development
Solutions offered in ZF2
More options
Lots of data
Lists of data rows
<?php
$query = "SELECT * FROM `contact` WHERE `age` > ? AND `gender` = ?"; $stmt = $pdo->prepare($query); $stmt->bindParam(1, $cleanAge); $stmt->bindParam(2, $cleanGender); $stmt->execute();
// A resultset of 63,992 entries stored in an array!!! $resultList = $stmt->fetchAll();
<?php
public function getSelectedContacts($age, $gender) { $resultSet = $this->tableGateway->select(array( 'age' => $age, 'gender' => $gender, )); return $resultSet; }
Iterators!!!
Loop with arrays• Data fetching time for 63992 of 250000 records:
2.14 seconds
• Data processing time for 63992 of 250000 records: 7.11 seconds
• Total time for 63992 of 250000 records: 9.25 seconds
• Memory consumption for 63992 of 250000 records: 217.75MB
Loop with Iterators• Data fetching time for 63992 of 250000 records:
0.92 seconds
• Data processing time for 63992 of 250000 records: 5.57 seconds
• Total time for 63992 of 250000 records: 6.49 seconds
• Memory consumption for 63992 of 250000 records: 0.25MB
Loop with Iterators• Data fetching time for 63992 of 250000 records:
0.92 seconds
• Data processing time for 63992 of 250000 records: 5.57 seconds
• Total time for 63992 of 250000 records: 6.49 seconds
• Memory consumption for 63992 of 250000 records: 0.25MB <-> 217.75MB
ScheduleIntroduction to ZF2
The array
Challenges in development
Solutions offered in ZF2
More options
Iterators
Interfaces
Responsibility Separation
Modules
ScheduleIntroduction to ZF2
The array
Challenges in development
Solutions offered in ZF2
More options
–Michelangelo van Dam
“You dislike arrays because you don’t know them well enough to love them”
Links
• Array functions http://php.net/manual/en/ref.array.php
• Iterator http://php.net/manual/en/class.iterator.php
• SPL Iterators and DataStructures http://php.net/spl/
PHPSheatSheetshttp://phpcheatsheets.com/index.php
http
s://w
ww.
flick
r.com
/pho
tos/
lwr/1
3442
5422
35
Contact us
in it2PROFESSIONAL PHP SERVICES
Michelangelo van Dam [email protected]
www.in2it.be
PHP Consulting - Training - QA
Thank youHave a great conference
http
://w
ww.
flick
r.com
/pho
tos/
drew
m/3
1918
7251
5