0

是否可以从爆炸中的特定数组中计数?

例子:

<?php
$number='This is the number of,5,6,7,8,9';
$collect=explode(",",$number);
print_r($collect);
?>

输出将是:

Array ( [0] => This is the number of [1] => 5 [2] => 6 [3] => 7 [4] => 8 [5] => 9 )

但我需要忽略第一个数组。意思是,我只想计算5,6,7,8,9并忽略"This is the number of"

4

4 回答 4

1
unset($collect[0]);

http://php.net/manual/en/function.unset.php

从数组中删除一个元素

于 2012-11-25T09:29:36.280 回答
1

您可以使用array_shift删除数组的第一个元素。

您写了“我想忽略第一个数组”,但您显然是指“数组元素”。请注意,“数组”是explode函数的整个输出。

于 2012-11-25T09:32:43.367 回答
1

有可能的。

您可以直接删除数组的第一个元素:

$number='This is the number of,5,6,7,8,9';
$collect=explode(",",$number);
unset($collect[0]);
print_r($collect);

But briefly, you should use regular expressions so you match only the numbers:

preg_match_all('/,(\d+)/, explode(",",$number), $collect);

see http://php.net/manual/en/function.preg-match-all.php

于 2012-11-25T09:36:24.183 回答
0

只需使用下面的代码而不是您的代码...这里我只是添加一个新行...第四行...

<?php
$number='This is the number of,5,6,7,8,9';
$collect=explode(",",$number);
array_shift($collect); // remove the first index from the array
print_r($collect);
?>

输出:

 Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 )
于 2012-11-25T09:35:26.783 回答