yolop Posted September 3, 2009 Share Posted September 3, 2009 i have array like that 111c6,vvvc6222,c6c6,444c6 i want to get the last indax that include the string "c6" how can i do this please???? now it need to be "4" array_search is not good because array_search get only 1 index' and in my problem i have 4 index Link to comment https://forums.phpfreaks.com/topic/172981-problem-in-array/ Share on other sites More sharing options...
ignace Posted September 3, 2009 Share Posted September 3, 2009 $search = 'c6'; $str = '111c6,vvvc6222,c6c6,444c6'; $parts = explode(',', $str); $sizeof = sizeof($parts); for ($i = $sizeof - 1; $i >= 0; --$i) { if (false !== strpos($str, $parts[$i])) { print $parts[$i]; break; } } Link to comment https://forums.phpfreaks.com/topic/172981-problem-in-array/#findComment-911685 Share on other sites More sharing options...
RussellReal Posted September 3, 2009 Share Posted September 3, 2009 <?php function getLastIndex($a,$s) { $a = array_reverse($a,true); foreach ($a as $k => $v) { if (strpos($v,$s) !== false) return $k; } return false; } $array = array('abcd1','cdef','gpd1'); $key = getLastIndex($array,'d1'); // $key should equal 2 ?> Link to comment https://forums.phpfreaks.com/topic/172981-problem-in-array/#findComment-911686 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.