tags:

views:

66

answers:

6

Hi I have an array result like this, example 1:

Array ( [0] =>15 [1] => 16 [2] => 17 [3] => 18 )

example 2:

Array ( [0] =>15 [1] => 16 [2] => 17 [3] => 18 [4] => 18 )

The first array ends at array[3] The second array ends at array[4] How to calculate where the array ends Is there any function to calculate this

+2  A: 

Use the count function:

count(myArray)

This will tell you how many elements you have in the array.

http://www.w3schools.com/php/func_array_count.asp

pheelicks
+1  A: 

If it's just a normal numerically indexed array, then you can just use count(), which gives you the number of elements in the array. Since arrays are zero-based by default, you'll need to subtract one to get the index of the final element:

$array = array(0 => 15, 1 => 16, 2 => 17, 3 => 18);
$index = count($array) - 1;
echo $array[$index];
Will Vousden
+5  A: 

(Directly copied from http://www.php.net/manual/en/function.count.php)

Depending on what do you mean by "end",

<?php
$yourArray = array(1=>'a', 7=>'b', 5=>'c');

print count($yourArray); // prints 3

end($yourArray);
print key($yourArray); // prints 5

print max(array_keys($yourArray)); // prints 7
?> 

For a normal array, just use count($a) - 1.

KennyTM
+1 for completion, very nice.
pinkgothic
+2  A: 

I think what you are trying to do is count the number of elements in an array?

If so this would be the count function. http://php.net/manual/en/function.count.php

RandyMorris
+2  A: 

Associative array / array-with-holes agnostic:

$lastElement = end($array);
$lastKey     = key($array); // only after end(); has set the internal array pointer!
pinkgothic
+2  A: 

If you only need the last value of the array,you can just use array_pop

$arr = array('a','b','c');
echo array_pop($arr); //get 'c'

For fun:

$a = range(1, 100000);
shuffle($a);
$ts = microtime(true);
echo end($a),"\n";
printf("End =%.6f\n", microtime(true) - $ts);


//$b = range(1, 100000)
//shuffle($b);
reset($a);
$ts = microtime(true);
echo array_pop($a),"\n";
printf("Array_Pop=%.6f\n", microtime(true) - $ts);

Result is:

68875
End=0.000289
68875
Array_Pop=0.000053
SpawnCxy
Just a heads-up: `end()` is faster if you don't actually need to pop the element off. :)
pinkgothic
well,let me have a test.
SpawnCxy
the result shows that `array_pop` is faster.
SpawnCxy
I'd like to see that test. Make sure you're `reset`-ing the array between tests, so `array_pop` cannot benefit from `end` having done half its work for it already. Also make sure your `array_pop` test isn't benefiting from getting a shorter array to work with in the long run. `array_pop` does what `end` does, except it *additionally* has to edit the array. By definition it has to be slower. :)
pinkgothic
(Benchmarking is not trivial.)
pinkgothic
plz see update.
SpawnCxy
Hmm. That's really strange. I've gotten the opposite result with this: http://pastebin.com/KzwbBbFE (For a console run, you may want to replace `<br />` with `\n`, of course.)
pinkgothic
AHA! That's interesting. Swap your cases. Sluggishness in PHP?
pinkgothic
I've check your testcase and I think there's maybe something wrong cuz `end` doesn't make the array shorter as `array_pop` does.
SpawnCxy
that means you made `array_pop` did more work.:)
SpawnCxy
Well, yeah, that's basically what I'm saying, after all. :P It's not 'something wrong', it's why `end` is faster than `array_pop` if you don't actually need to truncate the array. But, seriously. Reverse your cases - test `array_pop` first, then `end`, you'll notice it says `array_pop` is slower then. Guess this is why single-run benchmarks aren't considered reliable? (Was new to me, too, even if I've always avoided them.)
pinkgothic
No, I didn't make `array_pop` do more work. It natively truncates the array. `end` does not. That's why `end` is faster *if you don't actually need to pop the element off.* :3 Please see my original comment.
pinkgothic
wow,it did happen as you said...
SpawnCxy
Yeah - I have no idea what's happening internally, there. Wish I knew to explain it. I can only imagine PHP takes a bit to get going properly, so whatever is at the start of the script is bound to benchmark more sluggishly in single runs? I'm a bit scared now. :)) Anyway. Thank you for exploring that with me.
pinkgothic
my pleasure.:-)
SpawnCxy