[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

How to repeat range within an array

Lets say I have an array like this:

$array[]

1.txt
2.txt
3.txt
marker start.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
marker end.txt
10.txt
11.txt
12.txt
13.txt

How do I repeat the range between the markers and delete the marker rows so $array[] becomes:

1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
10.txt
11.txt
12.txt
13.txt
0
fcqmax
Asked:
fcqmax
  • 2
  • 2
1 Solution
 
JoachimMartinsenCommented:
This should do it:
<?php
 
function RemoveMarker($ArrayField) {
	if  (stristr(strtolower($ArrayField),"marker") == false) {
		return true;
	}
	return false;
}
 
$OldArray = array("1.txt","2.txt","3.txt","marker start.txt","4.txt","5.txt","6.txt","7.txt","8.txt","9.txt","marker end.txt","10.txt","11.txt","12.txt","13.txt");
 
$NewArray = array_values(array_filter($OldArray,"RemoveMarker"));
 
print_r($NewArray);
 
?>

Open in new window

0
 
fcqmaxAuthor Commented:
Thanks, that part is solved. But still looking for the range between the markers to be repeated.
0
 
JoachimMartinsenCommented:
Oh, sorry. I totally misunderstood your question.

Try this:
<?php
 
$OldArray = array("1.txt","2.txt","3.txt","marker start.txt","4.txt","5.txt","6.txt","7.txt","8.txt","9.txt","marker end.txt","10.txt","11.txt","12.txt","13.txt");
$NewArray = array();
 
$ArrayMarkerStart = array_search("marker start.txt",$OldArray);
$ArrayMarkerEnd = array_search("marker end.txt",$OldArray);
 
for ($i=0; $i<=$ArrayMarkerStart-1; $i++) { array_push($NewArray,$OldArray[$i]); }
for ($i=$ArrayMarkerStart+1; $i<=$ArrayMarkerEnd-1; $i++) { array_push($NewArray,$OldArray[$i]); }
for ($i=$ArrayMarkerStart+1; $i<=$ArrayMarkerEnd-1; $i++) { array_push($NewArray,$OldArray[$i]); }
for ($i=$ArrayMarkerEnd+1; $i<=count($OldArray)-1; $i++) { array_push($NewArray,$OldArray[$i]); }
 
print_r($NewArray);
 
?>

Open in new window

0
 
fcqmaxAuthor Commented:
Great! Working now!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now