?
Solved

Repeat ranges within an array

Posted on 2009-04-20
2
Medium Priority
?
575 Views
Last Modified: 2012-05-06
Lets say I have an array like this:

$array[] =

1
2
3
marker1start
4
5
6
marker1end
7
8
9
10
marker2start
11
12
marker2end
13
14
15
marker3start
16
17
18
marker3end
19
20


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

1
2
3
// [marker-deleted]
4
5
6
4
5
6
// [marker-deleted]
7
8
9
10
// [marker-deleted]
11
12
11
12
// [marker-deleted]
13
14
15
// [marker-deleted]
16
17
18
16
17
18
// [marker-deleted]
19
20
0
Comment
Question by:fcqmax
2 Comments
 
LVL 18

Accepted Solution

by:
Hube02 earned 2000 total points
ID: 24186125
I don't know if this is the best solution, but it works.

You could even nest markers as in the following example and it will do the work:

1
2
marker0start
3
marker1start
4
5
6
marker1end
7
8
9
10
marker2start
11
12
marker2end
13
14
15
marker3start
16
marker4start
17
marker4end
18
marker3end
19
marker0end
20

<?php
  
  $array = array(1,2,3,'marker1start',4,5,6,'marker1end',7,8,9,10,'marker2start',11,12,'marker2end',13,14,15,'marker3start',16,17,18,'marker3end',19,20);
  
  $markers = array();
  $output = array();
  $count = 0;
  foreach ($array as $key => $value) {
    if (preg_match('/^marker(\d+)start$/i', $value, $matches)) {
      $marker = $matches[1];
      $markers[$marker]['start'] = $count;
    } elseif (preg_match('/^marker(\d+)end$/i', $value, $matches)) {
      $marker = $matches[1];
      $markers[$marker]['end'] = $count;
      for ($i=$markers[$marker]['start']; $i<$markers[$marker]['end']; $i++) {
        $output[$count] = $output[$i];
        $count++;
      }
    } else {
      $output[$count] = $value;
      $count++;
    }
  }
 
 
  print_r($output);
  
?>

Open in new window

0
 

Author Closing Comment

by:fcqmax
ID: 31572227
Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question