Simple array iterators

These are simple algorithms that iterate through arrays.

Let’s look at first function. It takes one numeric array consisting of associated arrays as a first argument. In resulting array it leaves only keys specified in second argument array. It also leaves but turns into dates/times keys specified in the third argument. It can also take a format of date/time in forth argument.
function usefulArray(array $outerArray, array $keys, array $dateKeys, string $dateFormat='D, d M Y H:i:s')

{
    foreach ($outerArray as $number => $innerArray){
        foreach ($innerArray as $key => $value){
            if (in_array($key, $dateKeys)) {
                $outerArray[$number][$key] = date($dateFormat, $value);
            }
            elseif (!in_array($key, $keys)){
                unset( $outerArray[$number][$key]);
            }
        }
    }
    return $outerArray;
}
The second function does the same but it creates a new array instead of changing incoming array.
function usefulArray(array $outerArray, array $keys, array $dateKeys, string $dateFormat='D, d M Y H:i:s')
{
    $result = [];
    foreach ($outerArray as $number => $innerArray){
        foreach ($innerArray as $key => $value){
            if (in_array($key, $keys)){
                $result[$number][$key] = $value;
            }
            if (in_array($key, $dateKeys)) {
                $result[$number][$key] = date($dateFormat, $value);
            }
        }
    }
    return $result;
}
We cant test them. Let’s take testing data:
$array = [
    ['foo'=> 12340, 'bar'=> 'hello0', 'baz'=>'hi0', 'beep'=>'qwe0', 'boop'=>'asd0', 'biip'=>98760, 'baap'=>47560],
    ['foo'=> 12341, 'bar'=> 'hello1', 'baz'=>'hi1', 'beep'=>'qwe1', 'boop'=>'asd1', 'biip'=>98761, 'baap'=>47560],
    ['foo'=> 12342, 'bar'=> 'hello2', 'baz'=>'hi2', 'beep'=>'qwe2', 'boop'=>'asd2', 'biip'=>98762, 'baap'=>47560],
    ];
$keys = ['bar', 'boop'];
$dateKeys = ['foo', 'biip'];
Then we test it:
$result = usefulArray($array, $keys, $dateKeys);
var_dump($result);
We can test the performance. Call functions usefulArray and usefulArray2 and then create test functions:


function test() {
    $array = [
    ['foo'=> 12340, 'bar'=> 'hello0', 'baz'=>'hi0', 'beep'=>'qwe0', 'boop'=>'asd0', 'biip'=>98760, 'baap'=>47560],
    ['foo'=> 12341, 'bar'=> 'hello1', 'baz'=>'hi1', 'beep'=>'qwe1', 'boop'=>'asd1', 'biip'=>98761, 'baap'=>47560],
    ['foo'=> 12342, 'bar'=> 'hello2', 'baz'=>'hi2', 'beep'=>'qwe2', 'boop'=>'asd2', 'biip'=>98762, 'baap'=>47560],
    ];
$keys = ['bar', 'boop'];
$dateKeys = ['foo', 'biip'];
$result = usefulArray($array, $keys, $dateKeys);
}

function test1() {
    $array = [
    ['foo'=> 12340, 'bar'=> 'hello0', 'baz'=>'hi0', 'beep'=>'qwe0', 'boop'=>'asd0', 'biip'=>98760, 'baap'=>47560],
    ['foo'=> 12341, 'bar'=> 'hello1', 'baz'=>'hi1', 'beep'=>'qwe1', 'boop'=>'asd1', 'biip'=>98761, 'baap'=>47560],
    ['foo'=> 12342, 'bar'=> 'hello2', 'baz'=>'hi2', 'beep'=>'qwe2', 'boop'=>'asd2', 'biip'=>98762, 'baap'=>47560],
    ];
$keys = ['bar', 'boop'];
$dateKeys = ['foo', 'biip'];
$result = usefulArray($array, $keys, $dateKeys);
}
$array = [];
for ($i = 0; $i<1000; $i++){
    $time_pre = microtime(true);
    for ($i = 0; $i<1000; $i++){
        test();
    }
    $time_post = microtime(true);
    $exec_time = $time_post - $time_pre;
    array_push($array, $exec_time);
}
$result = array_sum($array)/count($array);

$array = [];
for ($i = 0; $i<1000; $i++){
    $time_pre = microtime(true);
    for ($i = 0; $i<1000; $i++){
        test1();
    }
    $time_post = microtime(true);
    $exec_time = $time_post - $time_pre;
    array_push($array, $exec_time);
}
$result1 = array_sum($array)/count($array);

echo $result . ' _____ ' . $result1;

If you like spoilers, both functions have almost the same performance, but function using unset (not creating new resulting array) worked about 10% faster in my tests.

Be the first to comment

Leave a Reply

Your email address will not be published.


*