0

Is it possible to count from a specific array in explode?

Example:

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

Output will be:

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

But I need to ignore the first array. Meaning, that I want to count only 5,6,7,8,9 and ignore "This is the number of".

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
Imran Abdur Rahim
  • 407
  • 1
  • 10
  • 21

4 Answers4

1
unset($collect[0]);

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

and

Delete an element from an array

Community
  • 1
  • 1
Jayson
  • 940
  • 8
  • 14
1

You can use array_shift to remove first element of the array.

You wrote "I want to ignore first array" but you obviously meant "array element". Note that "array" is the whole output of explode function.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
1

It is possible.

Directly you can remove the first element of the array:

$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

Plamen Nikolov
  • 2,643
  • 1
  • 13
  • 24
0

just use the below code instead of yours... here I am just adding a new line... the 4th one...

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

Output:

 Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 )
Naz
  • 2,520
  • 2
  • 16
  • 23