Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explode first 10 elements of string ignore the rest (comma separated)

Tags:

arrays

php

I want to explode the following string upto 10 elements

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

into array $arr.Tried with

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

It gives 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,o should be $arr[9]=j.

like image 393
Bharat Godam Avatar asked Mar 07 '23 09:03

Bharat Godam


1 Answers

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
like image 83
Himanshu Upadhyay Avatar answered Apr 28 '23 19:04

Himanshu Upadhyay