Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从特定索引的开头开始计算数组_Php_Arrays - Fatal编程技术网

Php 从特定索引的开头开始计算数组

Php 从特定索引的开头开始计算数组,php,arrays,Php,Arrays,PHP中是否有一个函数可以从数组的特定索引开始计数 例如: $array = array(1, 2, 3, 4, 'string', 5, 6, 7); var_dump(count($array)); // 8 items 功能与我需要的类似: $array = array(1, 2, 3, 4, 'string', 5, 6, 7); var_dump(countFromIndex($array, 2)); // 6 items (started from 3) 如果您试图获取从索引

PHP中是否有一个函数可以从数组的特定索引开始计数

例如:

$array = array(1, 2, 3, 4, 'string', 5, 6, 7);

var_dump(count($array)); // 8 items
功能与我需要的类似:

$array = array(1, 2, 3, 4, 'string', 5, 6, 7);

var_dump(countFromIndex($array, 2)); // 6 items (started from 3)

如果您试图获取从索引号2开始的项数,只需使用
count($array)-2

否,您需要编写一个。 最简单的方法(不是最好的,只是我能从头顶拉出来的)是

function countFrom($array, $indexFrom) {
$counter = 0;
$start = false;
foreach ($array as $k=>$v) {
    if ($k == $indexFrom) {
        $start = true;
    }
    if ($start) {
        $counter ++;
    }
}
return $counter;
}
或者,可能内存占用较少:

function countFrom($array, $indexFrom) {
$start = false;
$counter = 0; // experiment to see if this should be 0 or 1
foreach ($array as $k=>$v) {
    if ($k == $indexFrom) {
        $new = array_splice($array, $counter);
        return count($new);
    }
    $counter ++;
}
$total=count($array);
$count=0;
对于($i=2;$i<$total;$i++)
{
$count++;
}
更改
$i=2
以设置起点。

类似以下内容:

function countFromIndex($array, $index)
{
    $values = array_values($array);
    $count = count($values);
    $search = 0;
    for($i=0; $i<$count; $i++)
    {
        $search++;
        if($values[$i] === $index) break;
    }
    return $count-$search;
}

$array = array(1, 2, 3, 4, 'string', 5, 6, 7);
var_dump(countFromIndex($array, 2)); // 6 items (started from 3)
var_dump(countFromIndex($array, 'string')); // 3 items (started from 5)
var_dump(countFromIndex($array, 7)); // 0 items (started from ?)
函数countFromIndex($array,$index)
{
$values=array\u值($array);
$count=计数($value);
$search=0;

对于($i=0;$Itis不起作用,因为她正在寻找一个特定的索引,可能不知道它在哪里。@Grim…如果它真的是关于索引的,那么我的解决方案很好。如果想法是找到第一个与“2”匹配的值,那么是的,它不起作用,但是问题很不清楚,你不认为吗?@Grim…那么我认为没有理由拒绝投票回答低于零是的,很抱歉。我最初也误解了这个问题(也许我没有,谁知道),现在我不能改变我的投票,除非你编辑你的问题(我肯定会这样做)。问题尚不清楚。是否要从第二个位置的项开始计算第一次出现的2?非常不同的内容$pos=5;$count=count($array);$lenFromPos=$count-($pos+1);请澄清您所说的特定索引是什么意思?您是希望从第二个对象开始计数,还是从键为“2”的对象开始计数?请说明您在实现
countFromIndex
函数时遇到的问题。还不清楚您要求的是什么。默认情况下,PHP AFAIK中不存在此类函数。为什么不
$count=cont($array)-2;
?再次回到问题的模糊性,如果他们实际上希望从一个值而不是一个键中获得计数,那么这可能不起作用。不过很容易解决,将$k更改为$v,只是加强了问题的模糊性。
function countFromIndex($array, $index)
{
    $values = array_values($array);
    $count = count($values);
    $search = 0;
    for($i=0; $i<$count; $i++)
    {
        $search++;
        if($values[$i] === $index) break;
    }
    return $count-$search;
}

$array = array(1, 2, 3, 4, 'string', 5, 6, 7);
var_dump(countFromIndex($array, 2)); // 6 items (started from 3)
var_dump(countFromIndex($array, 'string')); // 3 items (started from 5)
var_dump(countFromIndex($array, 7)); // 0 items (started from ?)