Is an array like a database?
Posted: Sat Mar 31, 2007 3:55 pm
If I had an array that had price and id for each key and I wanted to find the lowest price in the array how could this be done? Can I query an array like a database?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
If you already have the data in an array, sort the array and then fetch the first element.
If you are getting the product and id from a database, why not only fetch just the relevant row?
e.g.
[syntax="sql"]
SELECT
id,
price
FROM
products
ORDER BY
price ASC
LIMIT 0, 1
Code: Select all
$db = new AllInPHPMemoryDB();
$db->statement('CREATE TABLE products (product varchar(255), price int(10));');
$db->statement('INSERT INTO products SET product = \'widget\', price = 499');
$db->statement('INSERT INTO products SET product = \'sprocket\', price = 599');
$db->statement('INSERT INTO products SET product = \'fubar\', price = 699');
$expensive = $db->query('SELECT * FROM products WHERE price > 550');Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]