Need some advice (searching large array)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Need some advice (searching large array)

Post by buckit »

here is what I have.

mysql database with a tables for prodcuts. this tables has 11000+ products. those 11,000 products are from 3 different distributors (identified in a column).

what I am doing is calling the database 3 different times for each distributor (distributor names: 21, 31, 41).

so I makes the call and get a recordset with ~5600 products.

I then download the up-to-date inventory file from the respective distributor. I parse the file into a multidimentional array. this array ($dist_data) has about 7800 arrays in it. each one with the product details.

I need to go through the database results and look in that array to see if the product exists... if it doesnt then it flags for review and be removed if the distributor no longer carries it. if it does exist then it check to see if the price needs to be updated or the qty needs to be updated. if it does then it processes that.

so here is my problem. right now I have it doing a while on the db results (so will loop ~5600 times) in each while is does a foreach on the array untill it finds a match.

is there a better way of doing this? so right now I am doing a foreach on an array with over 7000 sub arrays over 5600 times. I hope I explained myself... here is a code example:

Code: Select all

$rsr_products = $db->Execute("SELECT products_quantity, products_id, products_model, products_price, product_src_cost FROM products WHERE product_distributor = '21' or product_distributor = '20'");
    while(!$rsr_products->EOF){
        foreach($rsr_data as $key => $val){
 
            if($rsr_data[$key][0] == $rsr_products->fields['products_model']){
                $product_match = $rsr_data[$key];
                break;
            }
        }
    }
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Need some advice (searching large array)

Post by VladSun »

Convert your DB result array into a hash:

Code: Select all

 
$rsr_products = $db->Execute("SELECT products_quantity, products_id, products_model, products_price, product_src_cost FROM products WHERE product_distributor = '21' or product_distributor = '20'");
foreach ($rsr_products as $key => $row)
{
    $rsr_products[$row['products_model']] = $rsr_products[$key];
    unset($rsr_products[$key]);
}
Then you can easily find a product model row by using it as a key:

Code: Select all

$product_match = $rsr_products[$model_to_find];
Your example code doesn't match your explanation, though ...
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply