Zend Framework2
How arrays will save your project
in it2PROFESSIONAL PHP SERVICES
Michelangelo van Dam
PHP Consultant, Community Leader & Trainer
https://www.flickr.com/photos/akrabat/8784318813
Schedule
Introduction to ZF2
The array
Challenges in development
Solutions offered in ZF2
More options
Schedule
Introduction 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',
        'In2itSsoProvider',
        'In2itCrmDashboard',
        'In2itCrmContactManager',
        'In2itCrmOrderManager',
        'In2itCrmProductManager',
    ),
    '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/'
    )
);
<?php
namespace In2itCrmDashboard;
use ZendModuleManagerFeatureConfigProviderInterface;
use ZendMvcModuleRouteListener;
use ZendMvcMvcEvent;
class Module implements ConfigProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'ZendLoaderClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'ZendLoaderStandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
}
https://www.flickr.com/photos/dasprid/8147986307
Yaml
XML
INI
CSV
PHP
PHP
Schedule
Introduction 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"
}
*/
Schedule
Introduction 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 `gende
r` = ?";
$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
Schedule
Introduction to ZF2
The array
Challenges in development
Solutions offered in ZF2
More options
Iterators
Interfaces
Responsibility
Separation
Modules
Schedule
Introduction 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/
PHPSheatSheets
http://phpcheatsheets.com/index.php
https://www.flickr.com/photos/lwr/13442542235
Contact us
in it2PROFESSIONAL PHP SERVICES
Michelangelo van Dam
michelangelo@in2it.be
www.in2it.be
PHP Consulting - Training - QA
Thank you
Have a great conference
http://www.flickr.com/photos/drewm/3191872515

Zf2 how arrays will save your project