there are various common tasks in PHP which can be done in several ways.
this site aims to benchmark these different approaches to find the fastest solution to the problem.
sometimes there are builtin PHP functions which are (in the most cases) the fastest solutions.
but their options are limited so there might be some benchmarks which don't make sense to you at first.
time required (shorter is better)
rank / method / description / code
resulting output
<?php
// ------- remove array item by value with explode / join
// -- the setup
$workdata = range('a','z');
$item2remove = 'c';
// -- the code to test
$newworkdata = explode(',',str_replace($item2remove.',','',(join(',',$workdata))));
?>
Array
(
[0] => a
[1] => b
[2] => d
[3] => e
[4] => f
[5] => g
[6] => h
[7] => i
[8] => j
[9] => k
[10] => l
[11] => m
[12] => n
[13] => o
[14] => p
[15] => q
[16] => r
[17] => s
[18] => t
[19] => u
[20] => v
[21] => w
[22] => x
[23] => y
[24] => z
)
back to the PHP benchmark results
<?php
// ------- remove array item by value with foreach loop
// -- the setup
$workdata = range('a','z');
$item2remove = 'c';
// -- the code to test
foreach($workdata as $item)
{
if($item != $item2remove) $newworkdata[] = $item;
}
?>
Array
(
[0] => a
[1] => b
[2] => d
[3] => e
[4] => f
[5] => g
[6] => h
[7] => i
[8] => j
[9] => k
[10] => l
[11] => m
[12] => n
[13] => o
[14] => p
[15] => q
[16] => r
[17] => s
[18] => t
[19] => u
[20] => v
[21] => w
[22] => x
[23] => y
[24] => z
)
back to the PHP benchmark results
<?php
// ------- remove array item by value with fast for loop
// -- the setup
$workdata = range('a','z');
$item2remove = 'c';
// -- the code to test
$n = count($workdata);
for($i = 0; $i < $n; $workdata[$i] != $item2remove, $i++)
{
$newworkdata[] = $workdata[$i];
}
?>
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
[8] => i
[9] => j
[10] => k
[11] => l
[12] => m
[13] => n
[14] => o
[15] => p
[16] => q
[17] => r
[18] => s
[19] => t
[20] => u
[21] => v
[22] => w
[23] => x
[24] => y
[25] => z
)
back to the PHP benchmark results
<?php
// ------- remove array item by value with array filter
// -- the setup
$workdata = range('a','z');
$item2remove = 'c';
// -- extra code
function istrue($elem)
{
global $item2remove;
if($elem != $item2remove) return true;
else return false;
}
// -- the code to test
$newworkdata = array_filter($workdata, 'istrue');
?>
Array
(
[0] => a
[1] => b
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
[8] => i
[9] => j
[10] => k
[11] => l
[12] => m
[13] => n
[14] => o
[15] => p
[16] => q
[17] => r
[18] => s
[19] => t
[20] => u
[21] => v
[22] => w
[23] => x
[24] => y
[25] => z
)
back to the PHP benchmark results
<?php
// ------- remove array item by value with for loop
// -- the setup
$workdata = range('a','z');
$item2remove = 'c';
// -- the code to test
$n = count($workdata);
for($i = 0; $i < $n; $i++)
{
if($workdata[$i] != $item2remove) $newworkdata[] = $workdata[$i];
}
?>
Array
(
[0] => a
[1] => b
[2] => d
[3] => e
[4] => f
[5] => g
[6] => h
[7] => i
[8] => j
[9] => k
[10] => l
[11] => m
[12] => n
[13] => o
[14] => p
[15] => q
[16] => r
[17] => s
[18] => t
[19] => u
[20] => v
[21] => w
[22] => x
[23] => y
[24] => z
)
back to the PHP benchmark results
<?php
// ------- remove array item by value with array walk
// -- the setup
$workdata = range('a','z');
$item2remove = 'c';
// -- extra code
function addelement($elem)
{
global $newworkdata, $item2remove;
if($elem != $item2remove) $newworkdata[] = $elem;
}
// -- the code to test
array_walk($workdata, 'addelement');
?>
Array
(
[0] => a
[1] => b
[2] => d
[3] => e
[4] => f
[5] => g
[6] => h
[7] => i
[8] => j
[9] => k
[10] => l
[11] => m
[12] => n
[13] => o
[14] => p
[15] => q
[16] => r
[17] => s
[18] => t
[19] => u
[20] => v
[21] => w
[22] => x
[23] => y
[24] => z
)
back to the PHP benchmark results