8

I have an array:

$TaxIds=array(2) { [0]=> string(1) "5" [1]=> string(2) "10" } 

I need convert to this:

$TaxIds=array(2) { [0]=> int(5) [1]=> int(10) } 

simple way???

Uri Goren
  • 13,386
  • 6
  • 58
  • 110
Fatemeh Namkhah
  • 669
  • 1
  • 6
  • 22

9 Answers9

20

Simplified version of @george's answer would be:

$TaxIds = array_map('intval', $TaxIds);

More info at array_map

Although the question was asked a long time ago, it may be useful for someone

14

You can use array_map

$TaxIds = array_map(function($value) {
    return intval($value);
}, $TaxIds);
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
ghindle
  • 560
  • 2
  • 10
  • 3
    Since PHP 7.4 it's possible to write it with the arrow function syntax on one line: `$TaxIds = array_map(fn($value) => intval($value), $TaxIds);` – John Linhart Aug 30 '21 at 09:18
1

Simple solution with array_map

$TaxIds=array_map(function ($v) {return (int)$v;},$TaxIds);
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
0
array_walk($array, function(&$value, &$key) {
    settype($value, "integer");
});
Disha V.
  • 1,834
  • 1
  • 13
  • 21
0

You could for loop it

for($i = 0; $i < count($TaxIds);$i++) {
    $TaxIds[$i] = (int) $TaxIds[$i];
}
jmattheis
  • 10,494
  • 11
  • 46
  • 58
0

try this:

$TaxIds = ["5", "10"];
$TaxIds = array_map(function($elem) { return intval($elem); }, $TaxIds);
var_dump($TaxIds);

var_dump output:

array(2) { [0]=> int(5) [1]=> int(10) }
ghettovoice
  • 358
  • 1
  • 5
  • 11
0

There are a lot of solutions to this problem. Some of them have been already posted. You can use one other solution with foreach.

 foreach($TaxIds as &$taxId) {
    $taxId = intval($taxId);
 }

However, I don't think you need to cast it to integer. PHP will detect if you want to make numeric operation and cast it dynamically.

Robert
  • 19,800
  • 5
  • 55
  • 85
0
$TaxIds = array( "1" ,"5", "2" ,"10");
$TaxIds = array_map(function($arr) {
    return intval($arr);
}, $TaxIds);

var_dump($TaxIds);

Output

array (size=4)
  0 => int 1
  1 => int 5
  2 => int 2
  3 => int 10
Jakir Hossain
  • 2,457
  • 18
  • 23
-3

You could try this:

$arr = array("1", "2");
$my = json_encode($arr, JSON_NUMERIC_CHECK);
$dec = json_decode($my, true);
var_dump($dec);

Output: array(2) { [0]=> int(1) [1]=> int(2) }

Or, as others have suggested, you could use array_map:

$arr = array("1", "2");
$arr = array_map(function($value) {
    return intval($value);
}, $arr);
var_dump($arr);

Output: array(2) { [0]=> int(1) [1]=> int(2) }

Edit: Use the following if you are expecting actual string in the array:

$arr = array("1", "2");
$arr = array_map(function($value) {
       return is_numeric($value) ? intval($value) : $value
}, $arr);
Criesto
  • 1,985
  • 4
  • 32
  • 41
  • 1
    It's likely because you're converting to JSON unnecessarily – Zach Saucier Nov 27 '15 at 17:52
  • The OP hasn't indicated that the array will contain strings that can't be parsed as integers, or what needs to be done with such values. If this *is* something the OP needs, it can be accomplished very easily by adjusting the function being passed to `array_map`. The use of JSON serialization is still unnecessary. – Asad Saeeduddin Nov 27 '15 at 18:58
  • @Criesto `return is_numeric($value) ? intval($value) : $value` – Asad Saeeduddin Nov 27 '15 at 19:16