Jump to content


Photo

looking for an array function to find last KEY


  • Please log in to reply
4 replies to this topic

#1 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 08 August 2006 - 02:04 PM

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.
array
(
   0= blue
   1= NULL
   2= NULL
   3= red
   4= NULL
)

last key is 4, but it = NULL... i want key 3 to be found
Im not perfect ;) lol

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 August 2006 - 02:12 PM

You can get the last item in the array using the end function. You can also manually navigate an array using the prev/next functions.

#3 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 08 August 2006 - 02:25 PM

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
$myArray = array_filter($yourArray);
print_r($myArray);
would give
array
(
    [0] = blue
    [1] = red
)

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

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#4 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 08 August 2006 - 02:35 PM

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

$myArray = array_filter($yourArray);
print_r($myArray);
would give
array
(
    [0] = blue
    [1] = red
)

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


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

    $lastuser = end($handlename);
    while($lastuser == NULL)
    {
      $lastuser = prev($handlename);
    }

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)
Im not perfect ;) lol

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 08 August 2006 - 07:10 PM


i am using "array_unique()" so any duplicate entries in the array are turned to NULL...


No. Duplicate entries are removed by array_unique()

So
<?php
$a = array(blue,blue,blue,red,red);
$b = array_unique($a);
echo '<pre>', print_r($b, true), '</pre>';
?>
gives-->
[pre]
Array
(
    0 => blue
    3 => red
)
[/pre]


Then
<?php
end($b);
list ($key, $value) = each ($b);
echo $value ;
?>
gives--> red
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users