Skip to content

Instantly share code, notes, and snippets.

@makoru-hikage
Last active June 20, 2017 12:11
Show Gist options
  • Save makoru-hikage/398605d5c82cb00ef2eb5f91db74a084 to your computer and use it in GitHub Desktop.
Save makoru-hikage/398605d5c82cb00ef2eb5f91db74a084 to your computer and use it in GitHub Desktop.
Shows the use of continue 2 and the problem with switch inside a loop.
<?php
public function setSort(array $items){
$columnOrders = [];
foreach ($items as $item) {
$itemStartsWithAlpha = preg_match('/^\W[A-Za-z]*/', $item);
if (! $itemStartsWithAlpha) {
$column = substr($item, 1);
switch (substr($item, 0, 1)) {
case '+':
$columnOrders[$column] = 'asc';
continue 2;
break;
case '-':
$columnOrders[$column] = 'desc';
continue 2;
break;
default:
continue 2;
}
}
$columnOrders[$column] = 'asc';
}
$this->sort = $columnOrders;
return $this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment