【发布时间】:2018-10-11 11:39:21
【问题描述】:
假设我有以下数组:
$n[5] = "hello";
$n[10]= "goodbye";`
我想找出这个数组的最高索引。
在 Javascript 中,我可以执行 $n.length - 1,这将返回 10。
但在 PHP 中,count($n) 返回 2,即数组中的元素数。
那么如何获取数组的最高索引呢?
【问题讨论】:
假设我有以下数组:
$n[5] = "hello";
$n[10]= "goodbye";`
我想找出这个数组的最高索引。
在 Javascript 中,我可以执行 $n.length - 1,这将返回 10。
但在 PHP 中,count($n) 返回 2,即数组中的元素数。
那么如何获取数组的最高索引呢?
【问题讨论】:
【讨论】:
[].length。没有麻烦,对我来说 php 和 javascript 之间的区别很有趣。
$n.length 是一步,-1 是第二步。 :):)
for (i = 0; i < n.length; i++).... 所以只有 1 步 ;-)
$n = [];
$n[5] = "hello";
$n[10]= "goodbye";
// get the list of key
$keyList = array_keys($n);
// get the biggest key
$maxIndex = max($keyList);
echo $n[$maxIndex];
输出
goodbye
【讨论】: