Forum Moderators: coopster

Message Too Old, No Replies

Searching A Multi-Dimensional Array and Displaying Results Using PHP

         

akogo

2:34 pm on May 7, 2003 (gmt 0)

10+ Year Member



Here's a sample of the multi-dimensional array "database" I created:

$caraccessories = array (

'20302' => array('item_number' => '20302','item_name' => '30 Piece Emergency Tool Set','item_price' => '49.95','item_cat' => 'automotive','item_subcat' => 'car-accessories','item_nav'=> '$nav[ca]'),

'30415' => array('item_number' => '30415','item_name' => 'Roadside Travel Kit','item_price' => '12.95','item_cat' => 'automotive','item_subcat' => 'car-accessories','item_nav'=> '$nav[ca]')

);

I want php to find only item's that are priced (index=item_price) $12.95 or below and display the result in the browser.

Can this be done using only php with multi-dimensional arrays -- and without MYSQL?

cyberax

4:21 pm on May 7, 2003 (gmt 0)

10+ Year Member



I found this function that might help you sort your array, once its sorted by price you could just walk through all the records in order while price is less than 12.50:

$array[0]['name'] = "Niko";
$array[0]['age'] = 24;
$array[1]['name'] = "Dennis";
$array[1]['age'] = 34;

the fuction:

function array_csort($marray, $column) {
foreach ($marray as $row) {
$sortarr[] = $row[$column];
}
array_multisort($sortarr, $marray);
return $marray;
}

just use it like that:

$array = array_csort($array,"age");

akogo

5:31 pm on May 7, 2003 (gmt 0)

10+ Year Member



cyberax,
thanx, I'll play with this on my own arrays and see what happens. Hope I can make it work.
akogo

cyberax

6:10 pm on May 7, 2003 (gmt 0)

10+ Year Member



No probs... if you get stuck just have a look at the documentation on php dot net. There are quite a few examples on the array_multisort() function.