Jump to content

looking for an array function to find last KEY


leeming

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
Link to comment
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
Link to comment
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)
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.