Aaray bittiği yerde nasıl hesaplayabiliriz?

6 Cevap php

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

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

Örnek 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

6 Cevap

(Doğrudan http://www.php.net/manual/en/function.count.php kopyalanan)

Bağlı olarak "sonuna" ile ne demek istiyorsunuz,

<?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
?> 

Normal bir dizi için, sadece kullanmak count($a) - 1.

Sayısı işlevini kullanın:

count(myArray)

Bu dizide kaç unsurlar size söyleyecektir.

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

Bir dizideki öğe sayısını olduğu yapmak için çalışıyoruz ne düşünüyorsun?

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

Ilişkisel dizi / array-ile-delikleri agnostik:

$lastElement = end($array);
$lastKey     = key($array); // only after end(); has set the internal array pointer!

Sadece dizinin son değeri gerekiyorsa, sadece kullanabilirsiniz array_pop

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

Eğlenmek için:

$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);

Sonucudur:

68875
End=0.000289
68875
Array_Pop=0.000053