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
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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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? :/
Link to comment
Share on other sites

[code]<?php
//You forgot to edit this line when you changed the arrays name:
foreach($tmp as $k => $v) {
    $new[] = $array[$k];
}

//It needs to be:
foreach($tmp as $k => $v) {
    $new[] = $pricearray[$k];
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.