Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Performance of Foreach in foreach

8 views
Skip to first unread message

Max Schindler

unread,
Apr 12, 2013, 4:22:34 AM4/12/13
to
$aSmallSetOfFields = array(0 => 'title', 2 => 'link', 5 => 'category');
$aBigSetOfFields = array(0 => 'title', 1 => 'price', 3 => 'image', 4 => 'brand', 5 => 'category');

$aProducts = array(0 => array(0 => 'Sportscar', 1 => '25000.00', 2 => 'http://www...', 3 => 'car123.jpg', 4 => 'Carcompany', 5 => 'Sportscars'))


// Imagine there would be 500.000 Products in the $aProducts Array

$aResultArray = array();
foreach ($aProducts as $aProduct)
{
foreach ($aProduct as $iValueKey => $aProductValues)
{
if (array_key_exists($iValueKey, $aSmallSetOfFields)
{
// Add an Entry into the $aResultArray
// $aResultArray[] = .....
}
}
}

// 500.000 Products and 4 Fields
// I tested it and it took 11 Seconds to run through this.


$aResultArray = array();
foreach ($aProducts as $aProduct)
{
foreach ($aProduct as $iValueKey => $aProductValues)
{
if (array_key_exists($iValueKey, $aSmallSetOfFields)
{
// Add an Entry into the $aResultArray
// $aResultArray[] = .....
}
}
}

// 500.000 Products and 4 Fields it took about 50 Seconds to run through this.
// Does anyone know a better way to do this?
0 new messages