Created
April 5, 2017 02:39
-
-
Save Zetzumarshen/7cbf0caf48064053167fa914e2e464b8 to your computer and use it in GitHub Desktop.
split array if previous element is not same
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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