1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

$arr = array(
	array('projects_users' => array('project_id' => 1)),
	array('projects_users' => array('project_id' => 2)),
	array('projects_users' => array('project_id' => 3)),
	array('projects_users' => array('project_id' => 4)),
	array('projects_users' => array('project_id' => 5)),
	array('projects_users' => array('project_id' => 6)),
	array('projects_users' => array('project_id' => 7)),
	array('projects_users' => array('project_id' => 8)),
	array('projects_users' => array('project_id' => 9)),
	array('projects_users' => array('project_id' => 10)));

//// Test 1 ////

echo 'Test 1, Iterative ';
$arr1 = $arr;
$test_1_start = microtime(true);

$new_arr = array();
foreach ( $arr1 as $el ) {
    $new_arr[] = $el['projects_users']['project_id'];
}

$test_1_end = microtime(true);
echo '- ' . ( 1000000 * ( $test_1_end - $test_1_start ) ) . " microseconds\n";
unset($arr1, $test_1_start, $new_arr, $test_1_end);

//// Test 2 ////

echo 'Test 2, array_map ';
$arr2 = $arr;
$test_2_start = microtime(true);

$new_arr = array_map(function($e) { return $e['projects_users']['project_id']; }, $arr2);

$test_2_end = microtime(true);
echo '- ' . ( 1000000 * ( $test_2_end - $test_2_start ) ) . " microseconds\n";
unset($arr2, $test_2_start, $new_arr, $test_2_end);

//// Test 3 ////

echo 'Test 3, Iterative with reset ';
$arr3 = $arr;
$test_3_start = microtime(true);

$new_arr = array();
foreach ( $arr3 as $el ) {
    $new_arr[] = reset(reset($el));
}

$test_3_end = microtime(true);
echo '- ' . ( 1000000 * ( $test_3_end - $test_3_start ) ) . " microseconds\n";
unset($arr3, $test_3_start, $new_arr, $test_3_end);

//// Test 4 ////

echo 'Test 4, array_map with reset ';
$arr4 = $arr;
$test_4_start = microtime(true);

$new_arr = array_map(function($el) { return reset(reset($el)); }, $arr4);

$test_4_end = microtime(true);
echo '- ' . ( 1000000 * ( $test_4_end - $test_4_start ) ) . " microseconds\n";
unset($arr4, $test_4_start, $new_arr, $test_4_end);

?>