By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

Looping through array

P: n/a
Consider the following array and string:

$trail = array('products/veggies', 'products', 'services/cleaning');
$path = 'products/veggies/1243/more';

I am trying to write a function that matches $path with a value in
$trail. If no match is found, it should chop off "more" and compare
again. Then, chop off "1243" and compare again. Now a match is found:
"products/veggies". The function should return true.

Let's take another example: $path = 'services/industrial/gardening'.
There is no match for $path in $trail. Chopping off "gardening" leaves
"services/industrial". Again, no match. Chopping off "industrial"
leaves "services". No match will be found; the function should return
false.

This is what I have so far:

function in_trail($path) {
global $trail;

while ($path && !in_array($path, $trail)) {
$path = substr($path, 0, strrpos($path, '/'));
}

return $path ? true : false;
}

But this function always returns true! Could someone explain this to
me and tell me how to fix it? Your help is much appreciated :-)

Feb 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
de**************@gmail.com wrote:
Consider the following array and string:

$trail = array('products/veggies', 'products', 'services/cleaning');
$path = 'products/veggies/1243/more';

I am trying to write a function that matches $path with a value in
$trail. If no match is found, it should chop off "more" and compare
again. Then, chop off "1243" and compare again. Now a match is found:
"products/veggies". The function should return true.

Let's take another example: $path = 'services/industrial/gardening'.
There is no match for $path in $trail. Chopping off "gardening" leaves
"services/industrial". Again, no match. Chopping off "industrial"
leaves "services". No match will be found; the function should return
false.

This is what I have so far:

function in_trail($path) {
global $trail;

while ($path && !in_array($path, $trail)) {
$path = substr($path, 0, strrpos($path, '/'));
}

return $path ? true : false;
}

But this function always returns true! Could someone explain this to
me and tell me how to fix it? Your help is much appreciated :-)
I changed it up a little, the function I wrote passes the string by
reference and uses recursion.

// returns true on success, false on failure
// $path is altered in the calling scope
// remove the reference operator (&) if you'd
// rather that not happen
function in_trail($trail, &$path) {
if ( in_array($path, $trail) ) {
return true;
}
else {
// we can't reduce string anymore
if ( ($idx = strrpos($path, '/')) === false )
return false;

// truncate last dir
$path = substr($path, 0, $idx);

// use recursion to check new dirs
return in_trail($trail, $path);
}
}

I haven't tested this on a wide variety of strings/arrays, but it did
work with your example vars.

Hope it helps.

Curtis, http://dyersweb.com
Feb 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.