...

View Full Version : largest numerical index in array?



david_kw
03-30-2007, 07:38 PM
In PHP is there a function (or easy way not using a loop) to find the largest numerical index in an array?

In javascript I can do



var a = new Array();
a[5] = "x";
alert(a.length); // 6 representing 0-5


in php is there something like that?



$a = array();
$a[5] = "x";
echo array_length($a); // shows 6 (or even a function that shows 5)


Thanks,
david_kw

iLLin
03-30-2007, 07:42 PM
Here ya go... the manual owns all: end (http://us.php.net/manual/en/function.end.php)

david_kw
03-30-2007, 07:55 PM
I'm not sure that is exactly what I'm looking for although that is my fallback position. Here is a more complex example.



$a = array();
$a[5] = "x";
$a['y'] = "yes";
echo array_length($a); // shows 6 (or even a function that shows 5)


Wouldn't end point to 'y' in this case?

What I'd like is something that does the following (but I don't know if it exists)



public function checkArray() {
$a[3] = "rt";
$a[5] = "x";
$a['y'] = "yes";

$max = 0;
foreach ($a as $key => $value) {
echo "testing '$key': ";
if (is_integer($key) && $max < $key) {
$max = $key;
echo "is integer<br>";
} else {
echo "is not integer<br>";
}
}
echo "max is '$max'<br>";
}
}


david_kw

iLLin
03-30-2007, 08:09 PM
Yea your looking for the last "key" correct? Well if you look at that page I showed you, the most recent comment shows you how.



$lastkey = end(array_keys($arr));

iLLin
03-30-2007, 08:11 PM
Well I guess that assumes your keys are all numbers. I would sort your array and then call that function.

iLLin
03-30-2007, 08:22 PM
$array = array("3" => "third", "e" => "exactly", "2" => "two", "yes", "no", "for" => "each");

print_r($array);
//Array ( [3] => third [e] => exactly [2] => two [4] => yes [5] => no [for] => each )

echo "<br /><br />";
ksort($array);
print_r($array);
//Array ( [e] => exactly [for] => each [2] => two [3] => third [4] => yes [5] => no )

echo "<br /><br />";
echo end(array_keys($array));
//5


Outputs 5

david_kw
03-30-2007, 08:27 PM
It looks like the exact functionality I wanted doesn't exist. Of course, there are some workarounds I can use including the ones you suggested.

Thanks for the help. It can suck up a lot of time trying to find something that doesn't exist. :)

david_kw

iLLin
03-30-2007, 08:38 PM
Ok now go help me with my activeX object problem in the JS forum!

aedrin
03-30-2007, 10:06 PM
With problems like these, a different solution could maybe be more usable.

Perhaps you are looking for the wrong thing.



var a = new Array();
a[5] = "x";
alert(a.length); // 6 representing 0-5


This to me is illogical code.

If I set a[5] to a value in an empty array, a.length should return 1. Because it only contains 1 element.

What are you trying to achieve? Snippets might help.

david_kw
03-30-2007, 10:30 PM
This to me is illogical code.

I can understand what you mean, but this is working javascript code not necessarily how I want it to work (feel free to try it out).



<script type="text/javascript">
var a = new Array();
a[5] = "x";
alert(a.length); // 6 representing 0-5
</script>


So since I learned javascript first I'm looking for similarities as I learn PHP. In JS, an array is numerical and the length property is updated as elements are added to the array. You can also add properties to an object which don't add to the overall length. So you can do



<script type="text/javascript">
var a = new Array("a", "b", "c", "d");
a['alphabetStart'] = true;
for (var i = 0; i < a.length; i++) {
alert(a[i]);
}
</script>


And just get the numerical keys for the array (4 alerts with a-d). I could use count() to do something similar in PHP but I'd get a value of 5 since it has the extra element.

Anyway, it isn't a big deal. The count() function will work fine in most cases and if I really need that functionality it is easy to write. I just wanted to know if there was already something written since PHP has tons of predefined functionality.

david_kw

aedrin
03-30-2007, 10:45 PM
I can understand what you mean, but this is working javascript code not necessarily how I want it to work (feel free to try it out).

I was mostly commenting on JavaScript's behaviour. I apologize if it looked like it was aimed as a comment on your code.


Anyway, it isn't a big deal. The count() function will work fine in most cases and if I really need that functionality it is easy to write. I just wanted to know if there was already something written since PHP has tons of predefined functionality.

Understood.

That is usually the best way of doing things. Find out if PHP already does something like that. Mostly because PHP's functions would be much faster than writing your own. (Both coding time and execution time)

marek_mar
03-31-2007, 12:08 AM
There is a reason there is no function for it. Such a function would be pointless. Why would you need the largest numerical key? You don't need it to add a new value or to iterate over the array.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum