views:

50

answers:

1
+1  Q: 

PHP array parsing

I am wondering if there are any classes for parsing PHP arrays in the general sense that you are looking back and forth over elements matching search patterns.

For example, lets say that If element "xyz" was found then I want to search backwards through the array until element "abc" or "cba" is found (or X number of steps backward is reached).

So I'm not sure how to describe this other than "regex for arrays". Does anyone know of any classes for this? My goal is to find data patterns in arrays that contain elements indexed in a certain order.

+1  A: 

The standard array functions in PHP should get you pretty close: (assuming your array is $arr)

$key = array_search("xyz", $arr, true);
if($key != false){
   $subset = array_slice($arr, 0, $key);
   $search2 = array_search("abc", $subset, true);
}

If you want to search backwards (formally, backwards) you can do so by reversing the array before searching it with array_reverse, and if you want to set a limit on steps backwards you can make a smaller slice.

Mark E