norbertk Posted May 4, 2010 Share Posted May 4, 2010 I have the following array: array('time_0945' => "0", 'time_1000' => "2636", 'time_1015' => "2636", 'time_1030' => "0", 'time_1045' => "2637", 'time_1100' => "2637", 'time_1115' => "2637", 'time_1130' => "2636"); The way that this is displayed is in a table in cell row fashion. The index is the time slot and the value is either '0' for no appointment, or any other number for an appointment id. What I am trying to do is calculate the number of times that the unique appointment occurs in consecutive order so that I can set a rowspan for table cell. What I have tried is: using array_unique; however if the appointment reoccurs again, such as above, that won't work. I have also attempted creating another array from that, but I can't figure out a way to count the number of times a consecutive value appears. What I ultimately would like is an output of: array(1, 2, 1, 3, 1) I am open to suggestions. I tried playing around with this topic but haven't been able to get what I want. Link to comment https://forums.phpfreaks.com/topic/200627-help-with-sequential-values-within-an-array/ Share on other sites More sharing options...
anups Posted May 4, 2010 Share Posted May 4, 2010 USE "array_count_values" $arr = array('time_0945' => "0", 'time_1000' => "2636", 'time_1015' => "2636", 'time_1030' => "0", 'time_1045' => "2637", 'time_1100' => "2637", 'time_1115' => "2637", 'time_1130' => "2636"); $new = array_count_values($arr); echo "<pre>"; print_r($new); echo "</pre>"; refer :- http://in3.php.net/array_count_values Link to comment https://forums.phpfreaks.com/topic/200627-help-with-sequential-values-within-an-array/#findComment-1052819 Share on other sites More sharing options...
norbertk Posted May 4, 2010 Author Share Posted May 4, 2010 Thank you for the suggestion but I've already tried this. The problem with this function is that it will return the TOTAL count for the values, which is partially what I am trying to get at and where I am stuck. As I described above, because one id repeats itself later in the array, it can't be counted as consecutive. Link to comment https://forums.phpfreaks.com/topic/200627-help-with-sequential-values-within-an-array/#findComment-1052824 Share on other sites More sharing options...
roopurt18 Posted May 4, 2010 Share Posted May 4, 2010 How does a unique appointment ID show up more than once in the array? Something tells me you haven't modeled your data in the best manner possible. Link to comment https://forums.phpfreaks.com/topic/200627-help-with-sequential-values-within-an-array/#findComment-1052838 Share on other sites More sharing options...
norbertk Posted May 4, 2010 Author Share Posted May 4, 2010 Because the array is a time schedule, and the appointment has a gap. Link to comment https://forums.phpfreaks.com/topic/200627-help-with-sequential-values-within-an-array/#findComment-1052985 Share on other sites More sharing options...
norbertk Posted May 5, 2010 Author Share Posted May 5, 2010 This is my array: Array ( [time_0900] => 0 [time_0915] => 0 [time_0930] => 0 [time_0945] => 0 [time_1000] => 0 [time_1015] => 0 [time_1030] => 2581 [time_1045] => 2581 [time_1100] => 0 [time_1115] => 0 [time_1130] => 0 [time_1145] => 2581 [time_1200] => 2581 [time_1215] => 2581 [time_1230] => 0 [time_1245] => 0 [time_1300] => 0 [time_1315] => 0 [time_1330] => 0 [time_1345] => 0 [time_1400] => 2617 [time_1415] => 2617 [time_1430] => 0 [time_1445] => 0 [time_1500] => 0 [time_1515] => 2617 [time_1530] => 2617 [time_1545] => 2617 [time_1600] => 0 [time_1615] => 0 [time_1630] => 0 [time_1645] => 0 [time_1700] => 2634 [time_1715] => 2634 [time_1730] => 2634 [time_1745] => 0 [time_1800] => 2633 [time_1815] => 2633 [time_1830] => 2635 [time_1845] => 2635 [time_1900] => 0 [time_1915] => 2633 [time_1930] => 0 [time_1945] => 2635 [time_2000] => 0 [time_2015] => 0 [time_2030] => 0 [time_2045] => 0 ) And this is what I want from it: Array (1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 3, 1, 1, 1, 1, 3, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1) I just need it to combine the consecutive values, and array_count_values does not work because some values repeat themselves. Link to comment https://forums.phpfreaks.com/topic/200627-help-with-sequential-values-within-an-array/#findComment-1053381 Share on other sites More sharing options...
phpchamps Posted May 5, 2010 Share Posted May 5, 2010 I hope this will help you... $appointments = array('time_0945' => "0", 'time_1000' => "2636", 'time_1015' => "2636", 'time_1030' => "0", 'time_1045' => "2637", 'time_1100' => "2637", 'time_1115' => "2637", 'time_1130' => "2636"); $appointments = array_values($appointments); function sequential_count($arr){ $new_arr = array(); $cnt = 0; if(is_array($arr) && !empty($arr)){ for($i=0;$i<count($arr);$i++){ if(current($arr) == next($arr)){ $cnt++; }else{ $new_arr[] = $cnt; $cnt=1; } } return $new_arr; }else{ return false; } } echo "<pre>"; print_r(sequential_count($appointments)); echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/200627-help-with-sequential-values-within-an-array/#findComment-1053421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.