Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Vishaal's avatar

Convert string to array

Hello,

Current i have an string in mine DB: 1,2,3,4,5,6, How i can convert this in array and separate them from each? How i can take only 3 from this string?

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104
// creates an array
explode(',' $string)

// takes first three elements
array_slice($array, 0, 3)

// together 
array_slice(explode(',', $string), 0, 3)
3 likes

Please or to participate in this conversation.