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

  • SEARCH
  • Home
  • 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 6815947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:52:02+00:00 2026-05-26T20:52:02+00:00

$result = array(test => 21:00, test => 11:00, test => 19:00, test => 04:00);

  • 0
$result = array("test" => "21:00", "test" => "11:00", "test" => "19:00", "test" => "04:00");
$sortedArray = array();
foreach ($result as $test => $mealTime) {
    if () {
        array_push($sortedArray, $mealTime);
    }
}

This is what I have done right now. I would like to make a new array out of the $result array. And in this new array I would like it to be sorted out from the meal times.

As you see in the current $result array it is unsorted as 04:00 which is in the end of the array is lower than 21:00.

So the correct array would be: 04:00, 11:00, 19:00, 21:00 (in the above example).

How can I do this? The above code is just an example of what I tried and then got stuck at writing a if statement to it..

  • 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-05-26T20:52:03+00:00Added an answer on May 26, 2026 at 8:52 pm

    First of all: do you really need an index as a string? You always use “test”? Is this just an example?

    If you don’t need it, simply do this:

    One dimensional array

    <?php
    
    $result = array('21:00', '11:00', '19:00', '04:00', '01:00', '13:45', '02:45', '00:00');
    sort($result, SORT_STRING);
    
    var_dump($result);
    
    ?>
    

    Output:

    array(8) { [0]=> string(5) "00:00" [1]=> string(5) "01:00" [2]=> string(5) "02:45" [3]=> string(5) "04:00" [4]=> string(5) "11:00" [5]=> string(5) "13:45" [6]=> string(5) "19:00" [7]=> string(5) "21:00" } 
    

    Using a multi-dimensional array
    (based on the code you provided in the comment)

    This sample does what you want but it’s not the way I would do it. I recommend using a class instead a second array. This is much cleaner and uses a more advanced coding style. As far as I see from your example your are creating the arrays manually so it does not result in a big change.

    <?php
    
    // Define a custom compare function that uses the inner array of your
    // multi-dimensional array.
    function compareMealTime($a, $b)
    {
        return strcmp($a['MealTime'], $b['MealTime']);
    }
    
    $result = array();
    $result[0] = array('blabla' => 123123, 'MealTime' => '21:00');
    $result[1] = array('assd' => 123123, 'MealTime' => '02:00');
    $result[2] = array('blabsdsddla' => 123123, 'MealTime' => '00:00');
    $result[3] = array('bladddbla' => 123123, 'MealTime' => '04:00');
    
    uasort($result, 'compareMealTime');
    
    var_dump($result);
    
    ?>
    

    Output:

    array(4) { [2]=> array(2) { ["blabsdsddla"]=> int(123123) ["MealTime"]=> string(5) "00:00" } [1]=> array(2) { ["assd"]=> int(123123) ["MealTime"]=> string(5) "02:00" } [3]=> array(2) { ["bladddbla"]=> int(123123) ["MealTime"]=> string(5) "04:00" } [0]=> array(2) { ["blabla"]=> int(123123) ["MealTime"]=> string(5) "21:00" } } 
    

    Otherwise you can use sort(), asort() as Liam Allan suggested or uasort().

    Check Sorting Arrays for a comparison of these functions to find out the one that best fits your needs.

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

Sidebar

Related Questions

I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
could you tell me, why is this wrong? I have mytype test[2]; stringsstream result;
$result = $this->db->query($query)->result_array(); if($result[9]['b_code'] != FALSE) { $result_array = array( '0' => $result[0]['b_code'], '1'
I got a result set in array format and i want to make square
I have an array which has the contents as the result of an sql
I have an array DetailsArray in VB.NET which contains the result of a LINQ
How can I assign query result into array elements? This is my code: include('db.php');
I'm running CodeIgniter 2.0 and I have a test controller setup with this code
I've done a million if and foreach's in scripts before, but this one I
I'm a noob so please be patient I have to test an array, so

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.