Jump to content

Archived

This topic is now archived and is closed to further replies.

leeming

looking for an array function to find last KEY

Recommended Posts

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 NULL

eg.
[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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 NULL

hope that helps

Share this post


Link to post
Share on other sites
[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 NULL

hope 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)

Share this post


Link to post
Share on other sites
[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]<?php
end($b);
list ($key, $value) = each ($b);
echo $value ;
?>[/code]
gives--> red

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.