Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a comma separated string of numbers to an array of integers?

Tags:

arrays

php

Say I have the string 1,2,3,4,5 and I want to convert this to an array of integers - what would be the best way?

I know I can use explode to create an array with the string but I need the array items to be integers.

like image 245
mike Avatar asked Aug 25 '10 21:08

mike


1 Answers

You can use array_map to apply intval to each array item after you explode the string:

$string = "1,2,3,4,5";
$int_array = array_map("intval", explode(",", $string));
like image 186
Daniel Vandersluis Avatar answered Sep 23 '22 12:09

Daniel Vandersluis