TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   How to search array (http://www.talkphp.com/general/4082-how-search-array.html)

benton 03-27-2009 02:51 AM

How to search array
 
I want to code a buy one get one free option on my site. Initially, I wrote it so that half of the number of products were free, based on the lowest prices. So, if three products were $200 each and three were $100 each, the three $100 ones would be free. But my customers quickly pointed out they were not really getting one free. It should have been one $200 and two $100 for free. That's where I am at now. I can't figure out how to determine which ones should be free. Here's some pseudo code showing what I am doing.

PHP Code:

function IsFree($id) {
Array
(
    [
0] => Array
        (
            [
id] => 1961
            
[price] => 129.0000
            
[model] => 20
        
)

    [
1] => Array
        (
            [
id] => 1962
            
[price] => 179.0000
            
[model] => 24
        
)

    [
2] => Array
        (
            [
id] => 7047
            
[price] => 229.0000
            
[model] => 30
        
)

    [
3] => Array
        (
            [
id] => 19650
            
[price] => 299.0000
            
[model] => 30
        
)

    [
4] => Array
        (
            [
id] => 1989
            
[price] => 279.0000
            
[model] => 36
        
)

    [
5] => Array
        (
            [
id] => 23112
            
[price] => 339.0000
            
[model] => 36
        
)

    [
6] => Array
        (
            [
id] => 22399
            
[price] => 409.0000
            
[model] => 40
        
)
)
  return 
true or false;
}

for (
$i 0$i count($products); ++$i {
  if (
IsFree($products[$i]['id']))
   echo 
'Mark as free';


The IsFree function reads through the products and creates a multi array sorted on the model and then the price. I just showed the output of that array to, hopefully, make it clearer. If I wanted to just return the half of the products that have the lowest prices, I could just take that many from the beginning of the array. But I need to check whether there are duplicate model sizes listed. If there are, then the lowest price of that pair gets returned instead. I think part of the problem might be that the array is being created each time and the product being checked is a whole new search. In other words, there's no history of what has already been found. I thought of adding another array to track those but that made an already confusing situation worse.

Does anyone have any suggestions on how to tackle this problem?

dschreck 03-27-2009 08:27 AM

You mean something like this:
PHP Code:

<?php

$data 
= array();
$data[] = array('id'=>1961,'price'=>129.0000,'model'=>20);
$data[] = array('id'=>1962,'price'=>179.0000,'model'=>24);
$data[] = array('id'=>7047,'price'=>229.0000,'model'=>30);
$data[] = array('id'=>19650,'price'=>299.0000,'model'=>30);
$data[] = array('id'=>1989,'price'=>279.0000,'model'=>36);
$data[] = array('id'=>23112,'price'=>339.0000,'model'=>36);
$data[] = array('id'=>22399,'price'=>409.0000,'model'=>40);



function 
isFree($input
{
    
$output = array();
    foreach(
$input as $row)
    {
        if(!
array_key_exists($row['model'],$output) || array_key_exists($row['model'],$output) && $output[$row['model']]['price'] < $row['price'] )
        {
            
$output[$row['model']] = $row;
        }
    }

    return 
$output;


echo 
"Current selected items: \n".print_r($data,true)."\n";
echo 
"New selected items:\n".print_r(isFree($data),true)."\n";

outputs:
Code:

Current selected items:
Array
(
    [0] => Array
        (
            [id] => 1961
            [price] => 129
            [model] => 20
        )

    [1] => Array
        (
            [id] => 1962
            [price] => 179
            [model] => 24
        )

    [2] => Array
        (
            [id] => 7047
            [price] => 229
            [model] => 30
        )

    [3] => Array
        (
            [id] => 19650
            [price] => 299
            [model] => 30
        )

    [4] => Array
        (
            [id] => 1989
            [price] => 279
            [model] => 36
        )

    [5] => Array
        (
            [id] => 23112
            [price] => 339
            [model] => 36
        )

    [6] => Array
        (
            [id] => 22399
            [price] => 409
            [model] => 40
        )

)

New selected items:
Array
(
    [20] => Array
        (
            [id] => 1961
            [price] => 129
            [model] => 20
        )

    [24] => Array
        (
            [id] => 1962
            [price] => 179
            [model] => 24
        )

    [30] => Array
        (
            [id] => 19650
            [price] => 299
            [model] => 30
        )

    [36] => Array
        (
            [id] => 23112
            [price] => 339
            [model] => 36
        )

    [40] => Array
        (
            [id] => 22399
            [price] => 409
            [model] => 40
        )

)


benton 03-28-2009 12:09 AM

Thank you for the code but that doesn't output the result I am looking for. I didn't think about it when I originally posted but I probably should have listed what the output should be, for this example, so I will do that now. Given the following array, there are seven entries, which means there should be three marked as free, since it's buy one get one.
PHP Code:

$data[] = array('id'=>1961,'price'=>129.0000,'model'=>20); 
$data[] = array('id'=>1962,'price'=>179.0000,'model'=>24); 
$data[] = array('id'=>7047,'price'=>229.0000,'model'=>30); 
$data[] = array('id'=>19650,'price'=>299.0000,'model'=>30); 
$data[] = array('id'=>1989,'price'=>279.0000,'model'=>36); 
$data[] = array('id'=>23112,'price'=>339.0000,'model'=>36); 
$data[] = array('id'=>22399,'price'=>409.0000,'model'=>40); 

The three would be:

This one since it is the lowest price and not part of a pair (where a pair refers to items with the same model numbers)
PHP Code:

$data[] = array('id'=>1961,'price'=>129.0000,'model'=>20); 

This one since it is part of a pair and is the lowest of that pair
PHP Code:

$data[] = array('id'=>7047,'price'=>229.0000,'model'=>30); 

This one since it is part of a pair and is the lowest of that pair
PHP Code:

$data[] = array('id'=>1989,'price'=>279.0000,'model'=>36); 

So even though this one
PHP Code:

$data[] = array('id'=>1962,'price'=>179.0000,'model'=>24); 

is lower in price than the last two above it, they take precedence since they are part of a pair.

benton 03-29-2009 12:31 AM

I just wanted to say that I figured this out. I ended up creating a master array before looping through the products. If a product is found, I then delete it from the master array and count the number of free products found. I have to iterate through the array more times that is probably necessay but it works. Thanks for trying to help.


All times are GMT. The time now is 07:14 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0