Solved

# How to repeat range within an array

Posted on 2009-04-16
243 Views
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
Question by:fcqmax

LVL 6

Expert Comment

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);

?>
``````
0

Author Comment

Thanks, that part is solved. But still looking for the range between the markers to be repeated.
0

LVL 6

Accepted Solution

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);

?>
``````
0

Author Closing Comment

Great! Working now!
0

## Featured Post

### Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …