Re: Looping through array
Thanks, your function works great. However, it boggled my mind why it
didn't do the job in my website, then. After hacking away for like an
hour, it occured to me: the function should do more than just chopping
and comparing. Please consider:
$trail = array('products/veggies/winter', 'products/veggies',
'products');
$path_1 = 'products'; // true
$path_2 = 'products/423'; // false
$path_3 = 'products/veggies'; // true
$path_4 = 'products/meat'; // false
$path_5 = 'products/veggies/23'; // false
$path_6 = 'products/veggies/winter'; // true
$path_7 = 'products/veggies/winter/23/edit'; // true
$path_8 = 'products/meat/cow/dried/54/edit'; // false
The longest element in $trail is 'products/veggies/winter'. It has 3
parts (products, veggies and winter). I will call this element "A".
The function compares $path with $trail. if $path consists of 3 parts
or less, try finding a direct match. If there is a direct match,
return true. This is the case with $path_1, $path_3 and $path_6.
$path_2, $path_4 and $path_5 also have 3 or less parts, and there is
no direct match with $trail, so we return false.
If $path has more element-parts than A (in this case: 3), chop off the
extra parts and look if we now have a match. Two examples are $path_7
and $path_8:
- $path_7 has 5 parts, which is 2 more than A. We chop off "23/edit"
and compare "products/veggies/winter" to A, which will match and thus
return true
- $path_8 has 6 parts, which is 3 more than A. We chop off "dried/54/
edit" and compare "products/meat/cow" to A, which will return false
Could you please help me putting this into a function? I have no
trouble describing what should happen, but find it difficult
translating it into an efficient function.
Thanks in advance for any reply :-)
|