3
$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..

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
Karem
  • 17,615
  • 72
  • 178
  • 278

4 Answers4

1

Keep the first item from your list inside a variable called smallest. Then scan your list from second item (Position 1). Compare those with smallest (That is a if condition). If you find something smaller that smallest, change the value of smallest to that. When you done scanning the array for first run, add the smallest to new array, pop it from old array and repeat above step until original list is empty.

You should using built in one since the above method has a performance issue.

You might want to look at this link for demo on different sorting algorithm. It's written in JAVA but shouldn't be hard to convert JAVA to PHP

http://www.cs.ubc.ca/~harrison/Java/sorting-demo.html

mytempfw
  • 73
  • 7
1

It doesn't matter how you sort that array it won't work

try

var_dump($result);

this is what I get

array(1) { ["test"]=> string(5) "04:00" } 

By declaring an array with the same key field "test" in this case, just overwrites the previous value for the same key field

Essentially what you are doing when you create your array is the equivalent to this

$result["test"] = "21:00";
$result["test"] = "11:00";
$result["test"] = "19:00";
$result["test"] = "04:00";

I have changed your code to make it work as I think you want it to

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

    asort($result);

    foreach($result as $v) {
        echo "$v<br />";
    }
bumperbox
  • 10,166
  • 6
  • 43
  • 66
  • The real array is from the database, where it is $array["MealTime"] that is the index I want to sort out after, and so no i dont always use "test", so i cant do it like this. And thats why i need to compare/do actions inside the foreach(). – Karem Nov 17 '11 at 00:05
  • 1
    If it is coming from the database, then just use ORDER BY mealtime in your sql statement. – bumperbox Nov 17 '11 at 00:07
  • There's the twist bumperbox. I cant :) – Karem Nov 17 '11 at 00:12
  • cant you do something like sort the array after the array key/index "MealTime" ? – Karem Nov 17 '11 at 00:18
1

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.

CodeZombie
  • 5,367
  • 3
  • 30
  • 37
  • The real array is from the database, where it is $array["MealTime"] that is the index I want to sort out after, and so no i dont always use "test", so i cant do it like this. – Karem Nov 17 '11 at 00:05
  • Ok i thought this worked, then i tried with some new values. Here are they unsorted: 01:00, 13:45, 02:45, 00:00. Here are they sorted by sort() : 00:00, 13:45, 01:00, 02:45. <- It didnt got that right, as it should be: 00:00, 01:00, 02:45, 13:45 – Karem Nov 17 '11 at 00:14
  • @Karem: I just tested it, it works for me as expected. Check my updated sample. – CodeZombie Nov 17 '11 at 00:17
  • Ok yes your sample works, but as i told you the array is not only containing one index, but more. See this example, this does not work: http://codepad.org/Uo8x7IfQ – Karem Nov 17 '11 at 00:24
  • 1
    @Karem: This is a very important note that you never mentioned before. Please include it in your question. – CodeZombie Nov 17 '11 at 00:37
0

try the php function asort()

$sortedArray = asort($result);
Liam Allan
  • 1,115
  • 1
  • 8
  • 13
  • 2
    [`asort()`](http://php.net/asort) sorts the array in place, it doesn't return the sorted array. – rid Nov 16 '11 at 23:50