hchsk Posted May 2, 2009 Share Posted May 2, 2009 i have a list of values, deliminated by periods. I want to print the value to the left and right of the value given to the function. if the value given is at the end of the list of values, i want it to loop to the other side, and return the first value as adjacent. here is the code i have attempted, but is far from working, and does not even yet address the issue of "looping" the string being searched through. function opponents($team) { //ADD DELIMITERS $search = '.' . $team . '.'; //SEARCH FOR ME $me = strpos($GLOBALS['opponents'], $search); $myopponent = substr($GLOBALS['opponents'], ($me), ($me+2)); $myopponent2 = substr($GLOBALS['opponents'], ($me-2), ($me)); //GET RID OF DELIMINERS AND ME $myopponent = str_replace('.', '', $myopponent); $myopponent = str_replace($team, '', $myopponent); $myopponent2 = str_replace('.', '', $myopponent2); $myopponent2 = str_replace($team, '', $myopponent2); print $myopponent . 'and' . $myopponent2; } thanks for any help! Link to comment https://forums.phpfreaks.com/topic/156487-solved-getting-adjacent-values/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 Rather than the code, can you just give me an example of what you want accomplished? Thanks! Link to comment https://forums.phpfreaks.com/topic/156487-solved-getting-adjacent-values/#findComment-824040 Share on other sites More sharing options...
hchsk Posted May 2, 2009 Author Share Posted May 2, 2009 ok, haha, so if i have .1.2.5.6.8.9.10. and i call functionadjacent('6'); i want output: 5, 8 and if i call functionadjacent('10'); i want: 9, 1 Link to comment https://forums.phpfreaks.com/topic/156487-solved-getting-adjacent-values/#findComment-824050 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 Here's one I made from scratch. Please excuse the naming. I'm also not a fan of using $GLOBALS. Assuming $opponents is - 1.2.5.6.8.9.10 function findOpponents ($search, $opponents) { $opponents = explode('.', $opponents); $num_opponents = count($opponents); $location = array_search($search, $opponents); // if $location is false, then there are no opponents if ($location === false) return null; // special cases if ($location == 0) return array($opponents[1], $opponents[$num_opponents - 1]); if ($location == $num_opponents - 1) return array($opponents[$location - 1], $opponents[0]); return array($opponents[$location - 1], $opponents[$location + 1]); } That should return an array or null. Link to comment https://forums.phpfreaks.com/topic/156487-solved-getting-adjacent-values/#findComment-824062 Share on other sites More sharing options...
hchsk Posted May 2, 2009 Author Share Posted May 2, 2009 this worked better than anything i've written, although i modified it as i'm afraid i needed the global variable, if you could suggest a better method i would be glad to know and use it. i need it because i'm calling this script as part of a larger file that is contained in an include, and it needs to use a variable that is contained in the file itself. this is the modified and working code that i am using, but it still does not loop, findOpponents('1'); prints "and 2" and findOpponents('10'); seems to completely fail. function findOpponents($search) { $opponents = $GLOBALS['opponents']; $opponents = explode('.', $opponents); $num_opponents = count($opponents); $location = array_search($search, $opponents); // if $location is false, then there are no opponents if ($location === false) return null; // special cases if ($location == 0) return array($opponents[1], $opponents[$num_opponents - 1]); if ($location == $num_opponents - 1) return array($opponents[$location - 1], $opponents[0]); $opponents = array($opponents[$location - 1], $opponents[$location + 1]); print $opponents[0] . ' and ' . $opponents[1]; } Link to comment https://forums.phpfreaks.com/topic/156487-solved-getting-adjacent-values/#findComment-824074 Share on other sites More sharing options...
hchsk Posted May 2, 2009 Author Share Posted May 2, 2009 actually, i made a mistake there, but what i said otherwise holds true. code modified to: function findOpponents($search) { $opponents = $GLOBALS['opponents']; $opponents = explode('.', $opponents); $num_opponents = count($opponents); $location = array_search($search, $opponents); // if $location is false, then there are no opponents if ($location === false) return null; // special cases if ($location == 0) $opponents = array($opponents[1], $opponents[$num_opponents - 1]); if ($location == $num_opponents - 1) $opponents = array($opponents[$location - 1], $opponents[0]); $opponents = array($opponents[$location - 1], $opponents[$location + 1]); print $opponents[0] . ' and ' . $opponents[1]; } Link to comment https://forums.phpfreaks.com/topic/156487-solved-getting-adjacent-values/#findComment-824077 Share on other sites More sharing options...
hchsk Posted May 2, 2009 Author Share Posted May 2, 2009 im thinking the solution is in $opponents[0] = end($opponents); $opponents[] = $opponents[1]; for the special cases, but i can't quite seem to get it right, as if you add these at the beginning, if you are searching for the last in the list, it would catch the first showing of it, which would then end up printing "and 1" . . . this gets us the special cases //special cases if ($opponents[0] == '') { print 'aha';} if ($opponents[1] == '') { print 'aha';} this works for checking the last value. . . function findOpponents($search) { $opponents = $GLOBALS['opponents']; $opponents = explode('.', $opponents); $location = array_search($search, $opponents); // if $location is false, then there are no opponents if ($location === false) return null; // set opponents $opponentsarray = $opponents; $opponents = array($opponents[$location - 1], $opponents[$location + 1]); //special cases if ($opponents[0] == '') { print 'aha';} if ($opponents[1] == '') { $opponents[1] = $opponentsarray[1];} print $opponents[0] . ' and ' . $opponents[1]; } Link to comment https://forums.phpfreaks.com/topic/156487-solved-getting-adjacent-values/#findComment-824266 Share on other sites More sharing options...
hchsk Posted May 2, 2009 Author Share Posted May 2, 2009 solved: function findOpponents($search) { $opponents = $GLOBALS['opponents']; $opponents = explode('.', $opponents); $location = array_search($search, $opponents); // if $location is false, then there are no opponents if ($location === false) return null; // set opponents $opponentsarray = $opponents; $opponents = array($opponents[$location - 1], $opponents[$location + 1]); //special cases if ($opponents[0] == '') { $opponents[0] = $opponentsarray[count($opponentsarray)-1];} if ($opponents[1] == '') { $opponents[1] = $opponentsarray[1];} print $opponents[0] . ' and ' . $opponents[1]; } Link to comment https://forums.phpfreaks.com/topic/156487-solved-getting-adjacent-values/#findComment-824270 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.