0

I have an array.

$arr = array('10min', '25min', '35 min', '1 hour', '10-20min', '2 hours', '25-30min', '2-2 1/2 hours', '45min' );

I need the output in the following order.
10min
10-20min
25min
25-30min
35min
45min
1hour
2hours
2-2 1/2 hours

Is it possible to get in PHP? Thanks in advance.

5
  • 2
    It's almost definitely possible. What have you tried? Commented Jan 30, 2015 at 14:35
  • One way is to recreate the array with your order. Commented Jan 30, 2015 at 14:37
  • I tried to separate values with 'hours' to a new array. And then values having '-'(hyphen) to a different array and then the remaining to another one. But couldn't achieve it. Commented Jan 30, 2015 at 14:38
  • @Mihai: But I get the values from different tables in Mysql. The same function is used to get different info from db. So it is impossible to use ORDER BY. Commented Jan 30, 2015 at 14:40
  • It may be better to post that code you tried, using different arrays for different suffixes here, so one can see what went wrong ? (Can't write that function for you right now, have to step out). Commented Jan 30, 2015 at 14:48

2 Answers 2

1

You can do it using this code :)

<?php

        $arr = array('10min', '25min', '35 min', '1 hour', '10-20min', '2 hours', '25-30min', '2-2 1/2 hours', '45min');
        $m = [];
        $h = [];

        foreach ($arr as $a) {
            if (strpos($a,"min")) $m[] = $a;
            elseif(strpos($a,"hours") || strpos($a,"hour")) $h[] = $a;
        }

        foreach ($m as &$mm) $mm = str_replace("-","zzz",$mm);
        foreach ($h as &$hh) $hh = str_replace("-","zzz",$hh);

        natsort($m);
        natsort($h);

        foreach ($m as &$mm) $mm = str_replace("zzz","-",$mm);
        foreach ($h as &$hh) $hh = str_replace("zzz","-",$hh);

        $a = array_merge($m,$h);
        print_r($a);

?>
Sign up to request clarification or add additional context in comments.

Comments

0
<?php

$arr = array('10min', '25min', '35 min', '1 hour', '10-20min', '2 hours', '25-30min', '2-2 1/2 hours', '45min' );
echo $arr['0'], "<br>";
echo $arr['4'], "<br>";
echo $arr['1'], "<br>";
echo $arr['6'], "<br>";
echo $arr['8'], "<br>";
echo $arr['3'], "<br>";
echo $arr['5'], "<br>";
echo $arr['7'], "<br>";
?>

If that's particulary what you want to do, then here you go, if you asked for something like that as an example.. use foreach()

7 Comments

Even if I use conditions to echo inside a foreach() it is gonna give me the same result right? It will not sort the array.
Can you give me the template of what you are trying to do?
Template? What kind of template?
a real example of what you are trying to achieve.
Why not making all in minutes, then convert minutes to time(hours,mins,sec)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.