Jump to content

Ordering results - all of them not just 1


Anidazen

Recommended Posts

Hello.

I haven't been able to find any information on this, yet I would have thought it to be a common topic. Anyway hopefully someone can help me

I want to order some generated input for example by price.


All I have seen on the net how to sort an associative array. I definitely need to store and sort more than two fields. Surely there must be a way to order results?
Link to comment
https://forums.phpfreaks.com/topic/15116-ordering-results-all-of-them-not-just-1/
Share on other sites

Hows this?
[code]<?php
$array = array(array("ashop.com","A Shop","1.20","In Stock","5"),
array("bshop.com","B Shop","3.20","In Stock","3"),
array("cshop.com","C Shop","2.20","In Stock","10"),
array("dshop.com","D Shop","0.20","Out of Stock","8")
);

$tmp = array();

foreach($array as $k => $v) {
$tmp[$k] = $v[2];
}

asort($tmp);
$new = array();

foreach($tmp as $k => $v) {
$new[] = $array[$k];
}

echo "<pre>"; print_r($new); echo "</pre>";
?>[/code]
Heres a commented version:
[code]<?php
//An array of arrays containing our data
$array = array(array("ashop.com","A Shop","1.20","In Stock","5"),
array("bshop.com","B Shop","3.20","In Stock","3"),
array("cshop.com","C Shop","2.20","In Stock","10"),
array("dshop.com","D Shop","0.20","Out of Stock","8")
);

$tmp = array(); //A temporary array we will be using

//Loop through our main array filling our temp array with just the prices,
//setting the temp array keys the same as the keys in our main array.
foreach($array as $k => $v) {
$tmp[$k] = $v[2];
}

//Sort our temp array (of prices) from lowest to highest,
asort($tmp);
$new = array(); //An array that will contain all of the data in order

//Loop through our temp array of prices, filling our new array with the full contents of our original array,
//this time ordered by price
foreach($tmp as $k => $v) {
$new[] = $array[$k];
}

//Show the contents of our final array
echo "<pre>"; print_r($new); echo "</pre>";
?>[/code]
Here, $new[0] will allways hold the details of the entery with the cheapest price, so $new[0][1] holds the shop name, the shop URL is in $new[0][0] and $new[0][2] holds the price.
I couldn't get this to work. Probably just me messing it up.

Here's a snippet of code incase it helps further. I did $pricearray = array(); at the top, and then tried filling it up like this. Don't see why this failed.


[code]$pricearray[7] = array("Store Name", "http://www.storeurl.com/", $useprice, $useamount, $priceper, "In Stock");


$tmp = array();

foreach($pricearray as $k => $v) {
$tmp[$k] = $v[2];
}

asort($tmp);

$new = array();

foreach($tmp as $k => $v) {
$new[] = $array[$k];
}


echo "Best price: <a href='" . $new[0][1] . "'>" .  $new[0][0] . "</a> (" . $new[0][3] . " for \$" . $new[0][2] . ") - " . $new[0][5];
[/code]



Output I get is all fields I tried there blank. Any ideas? :/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.