How to explode the first 10 elements of a line ignore the rest (separated by comma)

I want to blow the next line to 10 elements

$str = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o";

to $ arr.Tried with array

$arr = explode(",", $str,10);

It gives the result as

Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => g
[7] => h
[8] => i
[9] => j,k,l,m,n,o
)

dont want $arr[9]=j,k,l,m,n,oto be $arr[9]=j.

+4
source share
1 answer

Here you go:

$str = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o";
$arr = explode(",", $str);
$output = array_slice($arr, 0, 10);   // returns a to j
+7
source

Source: https://habr.com/ru/post/1690420/


All Articles