03-27-2009, 08:27 AM
|
#2 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
|
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
)
)
|
|
|
|