Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9177413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:19:17+00:00 2026-06-17T17:19:17+00:00

Solved. Thanks for everyone who helped with it by answers or comments and especially

  • 0

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/>";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:19:18+00:00Added an answer on June 17, 2026 at 5:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: This has since been solved. Thanks to everyone who helped. Invoking the method
SOLVED: thanks to everyone for the fast answers I have 3 tables, i joined
SOLVED I misspelled the property department -> Department. Thanks for the answers and a
First of all, thanks to everyone who helps me, it is much appreciated! I
THIS PROBLEM IS NOW SOLVED. THANK YOU TO EVERYONE WHO REPLIED. Hello, I am
THANKS EVERYONE - SOLVED! I have the following code: SELECT region, division, SUM(staff_count) AS
[ANSWER] Thanks everyone! The code to solved the problem below is: SELECT ID FROM
MAJOR THANKS TO EVERYONE WHO POSTED TO TRY TO HELP. YOU GUYS ARE AWESOME!
SOLVED MY OWN QUESTION! THANKS EVERYONE FOR THE HELP :) Ok. I'm having trouble
SOLVED - Thanks for your help everyone! Before I get to my code, I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.