Skip to content

Instantly share code, notes, and snippets.

@Zetzumarshen
Created April 5, 2017 02:39
Show Gist options
  • Save Zetzumarshen/7cbf0caf48064053167fa914e2e464b8 to your computer and use it in GitHub Desktop.
Save Zetzumarshen/7cbf0caf48064053167fa914e2e464b8 to your computer and use it in GitHub Desktop.
split array if previous element is not same
function split_sorted()
{
$arr = array('1','2','2','3');
$output = array();
$previous = null;
$temp = array();
foreach($arr as $row){
if(isset($previous) === true and $previous !== $row){
$output[] = $temp;
$temp = array();
}
$temp[] = $row;
$previous = $row;
}
$output[] = $temp;
echo"<pre>";
print_r($output);
echo"</pre>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment