Reputation: 1325
Solved. Thanks for everyone who helped with it by answers or comments and especially for those who spent couple of minutes typing some written explanations with their code so I actually got what is happening :)
Just some newbie php question. I have troubles in solving how to make this working. Basically I just want to sort menu by price, which includes only the name and the price.
Menu.txt looks like this:
Meat,1
Salad,3
Juice,2
But after running the program it echoes:
Array Array
Array Array
Array Array
And I would like to have it printed like:
Meat,1
Juice,2
Salad,3
Which makes me think I cant use variables in array() just like that so I wonder how I should actually do it? Code is down below and everything else works well in my program except sorting by price (if I just print .txt file without trying to sort is goes fine etc..)
<?php
if (file_exists("menu.txt"))
{
$lines = file("menu.txt");
$howmanylines = count($lines);
for($i=0; $i < $lines; $i++) {
$oneline = explode(",",$lines[$i]);
$name = $oneline[0];
$price = $oneline[1];
$sortingbyprice = array(
array($name),
array($price)
);
array_multisort($sortingbyprice[0], $sortingbyprice[1], SORT_NUMERIC, SORT_ASC);
echo $sortingbyprice[0] . " ";
echo $sortingbyprice[1] . "<br/>";
}
}
Upvotes: 1
Views: 3857
Reputation: 2856
You're inputting arrays into an array and sorting everytime you input a new value into the array.
This code doesn't: first it iterates through the file, adding the menu items to an associative array using the following format: $sortingbyprice[product] = price
. Then it sorts the array and loops through the sorted array, generating an output (which, of course, can be altered to suit your needs).
To sort in ascending order:
<?php
if (file_exists("menu.txt"))
{
$lines = file("menu.txt");
$sortingbyprice = array();
foreach ($lines as $line)
{
$oneline = explode(",", $line);
$sortingbyprice[$oneline[0]] = $oneline[1];
}
// Sort the array, maintaining key associations.
asort($sortingbyprice, SORT_NUMERIC);
foreach ($sortingbyprice as $product=>$price)
{
echo $product." ".$price."<br />";
}
}
?>
If you would like to sort in descending order, you can use
// Sort the array, maintaining key associations.
arsort($sortingbyprice, SORT_NUMERIC);
In short: asort() for ascending sorts, arsort() for descending sorts.
Upvotes: 2
Reputation: 227310
If you want to sort the entire array, you need to do that outside of the for
loop. You need to use the loop to parse the file into an array, but then you need to sort it (using usort
) outside of the loop.
You are just sorting each individual element of the array as you are reading them in. You're not comparing them with the other elements, so your array_multisort
doesn't actually do anything.
<?php
if (file_exists("menu.txt")) {
// Create the array outside the loop
$sortingbyprice = array();
$lines = file("menu.txt");
$howmanylines = count($lines);
// Note we're using $howmanylines here
for($i=0; $i < $howmanylines; $i++) {
// add each row to the array
$sortingbyprice[] = explode(",", $lines[$i]);
}
// Sort the array by its price
usort($sortingbyprice, function($a, $b){
return $a[1] - $b[1];
});
// echo the array
for($i=0; $i < $howmanylines; $i++) {
$row = $sortingbyprice[$i];
echo $row[0].' '.$row[1].'<br/>';
}
}
DEMO: http://codepad.viper-7.com/0Qegab
Upvotes: 0
Reputation: 9324
There is an error where your putting your data into an array. This is the correct method:
$sortingbyprice[] = array('name'=>$oneline[0],'price'=>$oneline[1]);
//then to 'echo' a value within the array:
echo $sortingbyprice['name'];
to do every record you could do this:
foreach($sortingbyprice as $price){
echo $price['name'].': £'.$price['price'];
}
Upvotes: 0
Reputation: 3714
The way you're doing it right know you're just inserting another array into the array. Unless you're not looking to really add another array into the array you'll have to omit the array surrounding your values.
$sortingbyprice = array(
$oneline[0],
$oneline[1]
);
Upvotes: -1