Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help shifting array

Status
Not open for further replies.

GOSCO

Technical User
Sep 26, 2000
134
GB
Hi there,

How would you go about shifting the array so that the start of the array is the variable $startofweek

$daysofweek = array(Sun, Mon, Tue, Wed, Thu, Fri, Sat);

e.g
if $startofweek=Wed

then:

$daysofweek = array(Wed, Thu, Fri, Sat, Sun, Mon, Tue);

I was thinking a about using a while loop along these lines

while ($daysofweek[0] != $startofweek) {
array_shift($daysofweek);
}

Though I didn't like fact that it may loop indefinatley if $startofweek is assigned incorrectly.

Just wandered how a "real" developer would code it :)


 
Hi

I think you actually mean :
PHP:
[b]while[/b] [teal]([/teal][navy]$daysofweek[/navy][teal][[/teal][purple]0[/purple][teal]][/teal] [teal]!=[/teal] [navy]$startofweek[/navy][teal])[/teal] [teal]{[/teal]
    [COLOR=darkgoldenrod]array_push[/color][teal]([/teal][navy]$daysofweek[/navy][teal],[/teal][COLOR=darkgoldenrod]array_shift[/color][teal]([/teal][navy]$daysofweek[/navy][teal]));[/teal]
[teal]}[/teal]
Regarding a loop-less solution :
PHP:
[COLOR=darkgoldenrod]array_splice[/color][teal]([/teal][navy]$daysofweek[/navy][teal],[/teal][purple]10[/purple][teal],[/teal][purple]0[/purple][teal],[/teal][COLOR=darkgoldenrod]array_splice[/color][teal]([/teal][navy]$daysofweek[/navy][teal],[/teal][purple]0[/purple][teal],[/teal][COLOR=darkgoldenrod]array_search[/color][teal]([/teal][navy]$startofweek[/navy][teal],[/teal][navy]$daysofweek[/navy][teal])));[/teal]
Regarding how real developers would do it, I suppose they would just manipulate the index without altering the array. For example to list them :
PHP:
[navy]$startofweekindex[/navy][teal]=[/teal][COLOR=darkgoldenrod]array_search[/color][teal]([/teal][navy]$startofweek[/navy][teal],[/teal][navy]$daysofweek[/navy][teal]);[/teal]

[b]for[/b] [teal]([/teal][navy]$i[/navy][teal]=[/teal][purple]0[/purple][teal],[/teal][navy]$l[/navy][teal]=[/teal][COLOR=darkgoldenrod]count[/color][teal]([/teal][navy]$daysofweek[/navy][teal]);[/teal][navy]$i[/navy][teal]<[/teal][navy]$l[/navy][teal];[/teal][navy]$i[/navy][teal]++)[/teal]
  [b]echo[/b] [navy]$daysofweek[/navy][teal][([/teal][navy]$i[/navy][teal]+[/teal][navy]$startofweekindex[/navy][teal])%[/teal][navy]$l[/navy][teal]],[/teal][green][i]"\n"[/i][/green][teal];[/teal]

Feherke.
 
for a different approach to feherke's you could try this. it is computationally more expensive but it allows for the date to be given in multiple formats ('sun' or 'sunday') and to be returned the same way (change the date parameter);

Code:
<?php
print_r (returnnewarray('Wednesday'));
function returnNewArray($startofweek){
	$u = strtotime($startofweek);
	for($i=0; $i<7; $i++) $array[] = date('l', strtotime("+$i day", $u));
	return $array;
}
?>

all the other alternatives i came up with are variations on those already published by feherke.
 
Why doesn't the code below create $daysofweek[8]?


Code:
$startofweekindex=array_search($startofweek,$daysofweek);

for ($i=0,$l=count($daysofweek);$i<$l;$i++)
  echo $daysofweek[($i+$startofweekindex)%$l],"\n";

 
Hi

GOSCO said:
Why doesn't the code below create $daysofweek[8]?
PHP:
[navy]$startofweekindex[/navy][teal]=[/teal][COLOR=darkgoldenrod]array_search[/color][teal]([/teal][navy]$startofweek[/navy][teal],[/teal][navy]$daysofweek[/navy][teal]);[/teal]

[b]for[/b] [teal]([/teal][navy]$i[/navy][teal]=[/teal][purple]0[/purple][teal],[/teal][navy]$l[/navy][teal]=[/teal][COLOR=darkgoldenrod]count[/color][teal]([/teal][navy]$daysofweek[/navy][teal]);[/teal][navy]$i[/navy][teal]<[/teal][navy]$l[/navy][teal];[/teal][navy]$i[/navy][teal]++)[/teal]
  [b]echo[/b] [navy]$daysofweek[/navy][teal][([/teal][navy]$i[/navy][teal]+[/teal][navy]$startofweekindex[/navy][teal])[/teal][highlight][teal]%[/teal][navy]$l[/navy][/highlight][teal]],[/teal][green][i]"\n"[/i][/green][teal];[/teal]

Feherke.
 
Ahhh Modulus, sorry. :))

As you have guessed PHP is not my strong point. Thanks for your help.





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top