Explode comma separated integers to intvals? [duplicate]
Asked Answered
S

6

16

Say I have a string like so $thestring = "1,2,3,8,2".

If I explode(',', $thestring) it, I get an array of strings. How do I explode it to an array of integers instead?

Stoker answered 28/2, 2013 at 12:8 Comment(4)
will your string contain integers and only comma??Spoils
@User, only integers and commas, nothing elseStoker
Check this post: #9594265Slattern
@AndrasToth, right that is it.Stoker
K
44

array_map also could be used:

$s = "1,2,3,8,2";
$ints = array_map('intval', explode(',', $s ));
var_dump( $ints );

Output:

array(5) {
  [0]=>      int(1)
  [1]=>      int(2)
  [2]=>      int(3)
  [3]=>      int(8)
  [4]=>      int(2)
}

Example codepad.

Kerch answered 28/2, 2013 at 12:14 Comment(0)
A
12

Use something like this:

$data = explode( ',', $thestring );
array_walk( $data, 'intval' );

http://php.net/manual/en/function.array-walk.php

Aubrey answered 28/2, 2013 at 12:11 Comment(4)
Yes, I noticed I was wrong and was going to edit it back. But I find the PHP docs confusing, it says callable for first arg.Stoker
@AmigableClarkKant, see the link, callable is second, first is array. But anyway, yes, this is one of the biggest PHP problems. Some functions are with underscore, some are without. Some have one order, others have reverse. Strange.Aubrey
@Prof.Falken well, i think your thinking of array_map?Terchie
This is no longer working in php 5.3 (see comments at php.net/manual/en/function.array-walk.php) . Instead use $data = array_map('intval',$data);Mollescent
L
1

For the most part you shouldn't really need to (PHP is generally good with handling casting strings and floats/ints), but if it is absolutely necessary, you can array_walk with intval or floatval:

$arr = explode(',','1,2,3'); 
// use floatval if you think you are going to have decimals
array_walk($arr,'intval'); 
print_r($arr);

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

If you need something a bit more verbose, you can also look into settype:

$arr = explode(",","1,2,3");
function fn(&$a){settype($a,"int");}
array_walk($f,"fn");
print_r($f);

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

That could be particularly useful if you're trying to cast dynamically:

class Converter {
    public $type = 'int';
    public function cast(&$val){ settype($val, $this->type); }
}
$c = new Converter();

$arr = explode(",","1,2,3,0");
array_walk($arr,array($c, 'cast'));
print_r($arr);

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 0
)

// now using a bool
$c->type = 'bool';
$arr = explode(",","1,2,3,0");
array_walk($arr,array($c, 'cast'));
var_dump($arr); // using var_dump because the output is clearer.

array(4) {
  [0]=>
  bool(true)
  [1]=>
  bool(true)
  [2]=>
  bool(true)
  [3]=>
  bool(false)
}
Legging answered 28/2, 2013 at 12:13 Comment(0)
A
0

Since $thestring is an string then you will get an array of strings.

Just add (int) in front of the exploded values.

Or use the array_walk function:

$arr = explode(',', $thestring);
array_walk($arr, 'intval');
Arrearage answered 28/2, 2013 at 12:12 Comment(1)
doesn't work for me checkout this link codepad.org/WwpN4J2xPiscary
J
0
$thestring = "1,2,3,8,a,b,2";

$newArray = array();
$theArray = explode(",", $thestring);

print_r($theArray);

foreach ($theArray as $theData) {
  if (is_numeric($theData)) {
    $newArray[] = $theData;
  }
}

print_r($newArray);

// Output

Original array

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 8 [4] => a [5] => b [6] => 2 )

Numeric only array

 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 8 [4] => 2 ) 
Jovial answered 28/2, 2013 at 12:14 Comment(0)
T
0
$arr=explode(',', $thestring);
$newstr   = '';
foreach($arr as $key=>$val){
  $newstr .= $val; 
}
Treva answered 28/2, 2013 at 12:15 Comment(1)
I don't think it procudes the expected result, it will produce a string like 12382.Kerch

© 2022 - 2024 — McMap. All rights reserved.