【问题标题】:Number of indexes in an Array [duplicate]数组中的索引数[重复]
【发布时间】:2013-10-06 07:17:24
【问题描述】:

我想计算我的新数组有多少索引。

下面是我的代码截图。紫色框中的值是我的输出。我的 $High 数组列出了我的 accu.php 和 new.php 中的值(在要求内)。

现在,想使用名为 $result 的新数组列出我的 $High 数组的唯一值。然后,我想计算我的 $result 数组的索引。我已经尝试了 count() 和 sizeof() 函数,它仍然显示 9,如下所示。

下面是我的代码:

<?php
require("accu.php");
require("new.php");

$High = array ($Accu_High1,$Accu_High2,$Accu_High3,$Accu_High4,$Accu_High5,$new1,$new2,$new3,$new4,$new5,$new6,$new7,$new8,$new9,$new0);

$result = array_unique($High); // remove similar
$count = sizeof($result); //count

for ($int = 0; $int<=14; $int++)
{
    echo "<b>$int</b> $High[$int]<br>";
}

echo "<br><br><br><b>$count</b>";
?>

下面是我的输出:

0 22
1 32
2 33
3 32
4 32
5 30
6 31
7 31
8 31
9 30
10 23
11 30
12 31
13 30
14 30



9

【问题讨论】:

    标签: php arrays count indexing size


    【解决方案1】:

    数组索引是唯一的。所以count($array) 是您计算 'indexes' 所需的全部内容。

    但是,我不确定这个问题是否显示了对 PHP 数组的最少理解。 PHP 在线手册或一本书就是这里的答案。就像foreach 的工作原理和这些琐碎的事情一样。

    所以对你的数组使用var_dump 并分析输出。然后看看实际发生了什么。有很多数组函数/功能,你应该知道它们。

    【讨论】:

    • 这是我使用你的 var_dump 的输出。数组(6) { [0]=> 字符串(2) "22" [1]=> 字符串(2) "32" [2]=> 字符串(2) "33" [5]=> 字符串(2) "30" [6]=> 字符串(2) "31" [10]=> 字符串(2) "23" } 0 22 1 32 2 33 3 32 4 32 5 30 6 31 7 31 8 31 9 30 10 23 11 30 12 30 13 31 14 31
    • @coding.bird 如果不使用$result 变量,为什么要创建它?在您的打印函数中,您使用未通过array_unique 的相同旧变量。你知道吗?
    猜你喜欢
    • 2010-10-28
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2021-12-22
    • 2013-10-05
    • 2012-05-18
    相关资源
    最近更新 更多