JavaEar 专注于收集分享传播有价值的技术资料

sorting by smallest value in array inside of array in php

I have an array of menus (breakfast, lunch, dinner) with their start times (08:00:00, 11:00:00, 16:00:00), and I need to pull out the menu closest to now() in the future and know which menu it is. This is what I have:

$menus = array(
    array("breakfast", "08:00:00"), 
    array("lunch", "11:00:00"),
    array("dinner", "16:00:00")
    );

This is how I've been getting the soonest menu (assuming it's before 8am).

foreach($menus as $a) {
     $menu[] = $a[0];
     $time[] = $a[1];
}
echo min($time);

This gets me the soonest time 08:00:00, but I can't figure out how to attach the menu to this realization. Let's say it echoes 08:00:00, how can I get the $menu in there so I can also know that 08:00:00 corresponds to "breakfast"?

3个回答

    最佳答案
  1. why not

    $menus = array(
        '08:00:00' => 'breakfast'
        ...
        '16:00:00' => 'dinner'
    )
    

    Times are the key, values are the menu attached to that time.

    If you inside on using the two-array structure, then as long as your keys in both arrays have 1:1 correspondence in positions, you can find the key for that value, then look up in the other array:

    $min = min($time);
    $key = array_keys($time, $min);
    $menu = $menu[$key];
    
  2. 参考答案2
  3. While I that the array structure can be made better, I was just curious to check how to do it with your specific data-set, so here goes:

        $menus = array(
            array("breakfast","08:00:00"), 
            array("lunch","11:00:00"),
            array("dinner","16:00:00")
        );
    
        foreach($menus as $a) {
           $menu[] = $a[0];
           $time[] = $a[1];
        }
    

    Considering this is how far you have reached so far, you can complete it by having a simple iterative function:

        function getMenu($time,$menus)
        {
            foreach($menus as $menu)
            {
                if($menu[1] == $time) return($menu[0]);
            }
        }
    

    And you can call it so: echo getMenu(min($time),$menus);

    Not optimal, and the data structure leaves much to be desired, but will get the job done...

  4. 参考答案3
  5. To sort the array by the lowest value, preserving the rest the keys, you can try the following:

    uasort($menus, function($a, $b) { 
        return $a[1] - $b[1]; 
    });
    

    For example, if you run the above for the following array

    $menus = array(
        array("dinner", "16:00:00"),
        array("breakfast", "08:00:00"), 
        array("lunch", "11:00:00"),
        array("brunch", "09:30:00")
        );
    

    you will get the following result:

    array(4) {
      [1]=>
      array(2) {
        [0]=>
        string(9) "breakfast"
        [1]=>
        string(8) "08:00:00"
      }
      [3]=>
      array(2) {
        [0]=>
        string(6) "brunch"
        [1]=>
        string(8) "09:30:00"
      }
      [2]=>
      array(2) {
        [0]=>
        string(5) "lunch"
        [1]=>
        string(8) "11:00:00"
      }
      [0]=>
      array(2) {
        [0]=>
        string(6) "dinner"
        [1]=>
        string(8) "16:00:00"
      }
    }