0
      // sort by day date keys
      ksort($unavailable);
      // sort time blocks of day by start_time
      foreach($unavailable as $each) {
        usort($each['blocks'], function($a, $b) {
          return strcmp($a->start_time, $b->start_time);
        });
      }

As you can see we are trying to sort an array by keys, then the blocks within an array by the value start_time

This is how the array looks like

[
  "2015-04-25" => [
    "blocks" => [
      $object1,
      $object2,
      $object3
    ]
  ]
]

After some debugging I realized that the problem is the modifications to blocks is not reflected in the original $unavailable array, it is not referencing the same array it seems...

For example:

foreach($unavailable as $each) {
  $each['blocks'] = null;
}

// $unavaiable[$date]['blocks'] still has original object(s)

What is the solution?

2 Answers 2

1

Solution is:

foreach ($unavailable as &$each) // see that & here?

Adding & to $each means that all changes made to $each will be applied to elements of $unavailable.

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

Comments

0

Reference it to the parent

foreach($unavailable as $key => $each) {
    usort($unavailable[$key]['blocks'], function($a, $b) {
      return strcmp($a->start_time, $b->start_time);
    });
  }

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.