1

Okay I have an array like this:

array {
    [0] => array {
               [0] => array {
                         ["Time"] => "01:00:00"
                      }
               [1] => array {
                         ["Time"] => "00:00:00"
                      }
    }
    [1] => array {
               [0] => array {
                         ["Time"] => "01:00:00"
                      }
               [1] => array {
                         ["Time"] => "00:00:00"
                      }
    }
}

Now what I want to do is sort the inner inner array (the one with the time values) by the time values. Just so that inner array is sorted by the time values. How would I do this?

I do have PHP 5.3 installed if that is helpful.

4 Answers 4

2
foreach ( $array as $key => &$part )
{
    usort( &$part, function( $a, $b ) {
        return strtotime($a['Time'])-strtotime($b['Time']) > 0;
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

The & before $part is deprecated, therefore I can't choose it as the final answer, thanks for your help though!
1

Try this:

$arr = array(array(array("Time" => "01:00:00"), array("Time" => "02:00:00"), array("Time" => "00:00:00")),
             array(array("Time" => "02:00:00"), array("Time" => "01:00:00"), array("Time" => "00:00:00")));

$c = count($arr);
for ($i = 0; $i < $c; $i++) {
    sort($arr[$i]);
}

var_dump($arr); // outputs desired array

Comments

0
function custom_map( $array ) {
    usort( $array, function( $a, $b ) {
        return strtotime($a['Time'])-strtotime($b['Time']) > 0;
    });
    return $array;
}

$new_array = array_map( "custom_map", $array );

https://www.php.net/manual/en/function.usort.php

https://www.php.net/manual/en/function.array-map.php

7 Comments

No that's sorting the outermost array. I just want to sort the arrays that contain the Time value. (The outer array order should be unaffected).
Sorry, I realized that and worked on an update but you beat me to it. Take a look at this.
Warning: strtotime() expects parameter 1 to be string, array given
Yeah, as I updated the code I inadvertently removed ['Time'] at the end. Please try the updated code.
Okay that now leaves the first set of arrays in but removes the ones which contain the value Time and replaces them with NULL.
|
0

I don't think it gets any simpler to sort the second level -- iterate the first level and call sort() on each occurrence. Because your string are presumably hh:ii:ss, it is completely safe to compare them as simple strings (no need to strtotime()).

Code: (Demo)

$array = [
    [
        ['Time' => "01:00:00"],
        ['Time' => "00:00:00"]
    ],
    [
        ['Time' => "01:00:00"],
        ['Time' => "00:00:00"]
    ]
];

array_walk($array, fn(&$v) => sort($v));
var_export($array);

Output:

array (
  0 => 
  array (
    0 => 
    array (
      'Time' => '00:00:00',
    ),
    1 => 
    array (
      'Time' => '01:00:00',
    ),
  ),
  1 => 
  array (
    0 => 
    array (
      'Time' => '01:00:00',
    ),
    1 => 
    array (
      'Time' => '00:00:00',
    ),
  ),
)

Comments

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.