mercredi 8 avril 2015

Search for array in array of arrays


Vote count:

0




I've found a few questions about searching arrays but none seem to satisfy my needs.


Basically I'm importing a large set of data from a CSV. I have:





$categories = array(
[4] => array([1] => root_category, [2] => child_category, [3] => another_child, [4] => last_category),
[8] => array([5] => root_category, [6] => child_category, [7] => another_child, [8] => last_category),
[12] => array([9] => root_category, [10] => child_category, [11] => another_child, [12] => last_category),
);

// Category to insert:

$insert_category = array([1] => root_category, [2] => child_category, [3] => another_child, [4] => last_category);



I need to see if $insert_category already exists in $categories and then I need to return the key (which is the end ID of the category hierarchy) of the $categories array that matches.


In the $categories I have around 7500 rows of categories, a foreach loop over the $categories for each $insert_category is hitting the max execution time so I was wondering if there is a more efficient way of doing this?


I'm currently using something like this:





if(count($categories) > 0)
{
foreach($categories as $key => $category)
{
if(count(array_diff($needle, $category)) === 0)
{
return $key;
}
}
}



Which is giving me "Maximum execution time of 300 seconds exceeded".


Any help on this is much appreciated, been stuck for about 5 days now! Thanks in advance.



asked 29 secs ago

dirk

32






Search for array in array of arrays

Aucun commentaire:

Enregistrer un commentaire