leeming Posted August 8, 2006 Share Posted August 8, 2006 i have looked up and down on php.net on the "array" list, but cant seem to find a function that will work for what i have...i am using "array_unique()" so any duplicate entries in the array are turned to NULL...i am basicly trying to find the last KEY that doesnt have the value of NULLeg.[code]array( 0= blue 1= NULL 2= NULL 3= red 4= NULL)[/code]last key is 4, but it = NULL... i want key 3 to be found Quote Link to comment https://forums.phpfreaks.com/topic/16896-looking-for-an-array-function-to-find-last-key/ Share on other sites More sharing options...
wildteen88 Posted August 8, 2006 Share Posted August 8, 2006 You can get the last item in the array using the [url=http://www.php.net/end]end[/url] function. You can also manually navigate an array using the [url=http://www.php.net/prev]prev[/url]/[url=http://www.php.net/next]next[/url] functions. Quote Link to comment https://forums.phpfreaks.com/topic/16896-looking-for-an-array-function-to-find-last-key/#findComment-71151 Share on other sites More sharing options...
paul2463 Posted August 8, 2006 Share Posted August 8, 2006 Hi you could try array_filter() without a callback function, if i got this right from the manual, taking your example above and call the array $yourArray we can get[code]$myArray = array_filter($yourArray);print_r($myArray); [/code]would give[code]array( [0] = blue [1] = red)[/code]so then you would just have to look for the last item in $myArray as it will not be NULL, because array_filter() will remove all the entries of input that are equal to FALSE i.e NULLhope that helps Quote Link to comment https://forums.phpfreaks.com/topic/16896-looking-for-an-array-function-to-find-last-key/#findComment-71164 Share on other sites More sharing options...
leeming Posted August 8, 2006 Author Share Posted August 8, 2006 [quote author=paul2463 link=topic=103418.msg411763#msg411763 date=1155047153]Hi you could try array_filter() without a callback function, if i got this right from the manual, taking your example above and call the array $yourArray we can get[code]$myArray = array_filter($yourArray);print_r($myArray); [/code]would give[code]array( [0] = blue [1] = red)[/code]so then you would just have to look for the last item in $myArray as it will not be NULL, because array_filter() will remove all the entries of input that are equal to FALSE i.e NULLhope that helps[/quote]thanks that was much more helpfull than the previous reply (sorry, but i only posted for help cos i was stressing, and couldnt get it right)... but i did get this with a bit of stressing out lol[code] $lastuser = end($handlename); while($lastuser == NULL) { $lastuser = prev($handlename); }[/code]but paul's (if works) woulda been easier to do*edit* just looked at that function, sorry no, wouldnt work.. (im all confused now, but what i have what i said above hopefully should work) Quote Link to comment https://forums.phpfreaks.com/topic/16896-looking-for-an-array-function-to-find-last-key/#findComment-71174 Share on other sites More sharing options...
Barand Posted August 8, 2006 Share Posted August 8, 2006 [quote author=leeming link=topic=103418.msg411739#msg411739 date=1155045850]i am using "array_unique()" so any duplicate entries in the array are turned to NULL...[/quote]No. Duplicate entries are removed by array_unique()So[code]<?php$a = array(blue,blue,blue,red,red);$b = array_unique($a);echo '<pre>', print_r($b, true), '</pre>';?>[/code]gives-->[pre]Array( 0 => blue 3 => red)[/pre]Then[code]<?phpend($b);list ($key, $value) = each ($b);echo $value ;?>[/code]gives--> red Quote Link to comment https://forums.phpfreaks.com/topic/16896-looking-for-an-array-function-to-find-last-key/#findComment-71315 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.