Jump to content

help with sequential values within an array


norbertk

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.