2

Given an array, how do I convert array starting at key [1] from type string to type int?

Array
(
  [0] => "id"
  [1] => "6086220176"
  [2] => "6542925762"
  [3] => "6623113406"
  [4] => "6702782948"
)

I've checked out related question how to convert array values from string to int? already, but I would like to skip the first key "id" int he conversion, and not all keys in array!

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user1899415
  • 3,015
  • 7
  • 22
  • 31
  • Do you want to convert based on the key value (being >=1) or based on the value itself (being of numeric type)? What have you attempted so far to make this happen? – Mike Brant Nov 08 '13 at 16:53
  • 1
    I just have to ask ... why? PHP is loosely typed, it makes pretty much no difference how the data is stored in the array, just cast it (if necessary) as and when you need to use it, surely? – CD001 Nov 08 '13 at 16:55
  • @CD001 Well it has its uses as a cheap and easy sanitatizer. For example i have a pagination script and the target urls have the `page=n` on them and it's cheap and easy to just cast `page` as int. – CrayonViolent Nov 08 '13 at 17:01
  • 1
    @CrayonViolent ... I'm not saying don't cast to (int), (float) or whatever ... but I'm not sure I can see the value of casting all the values in an array to a specific type and stuffing them back into the array ... surely at some point you're doing something with them and at *that* moment why not cast them? `if($something === (int) $array[2]) { ... }` for example... – CD001 Nov 08 '13 at 17:05
  • Is `$array[0]` "id" the literal string "id" or is it itself a number and you're just using a placeholder example? reason i ask is because several of the answers below won't work if `[0]` itself is also numeric @user1899415 – CrayonViolent Nov 08 '13 at 17:07
  • @CD001 fair enough, i guess i didn't read your comment hard enough – CrayonViolent Nov 08 '13 at 17:09

9 Answers9

5
array_walk($array, function (&$value) {
    if (ctype_digit($value)) {
        $value = (int) $value;
    }
});
var_export($array);

Output:

array (
  0 => 'id',
  1 => 6086220176,
  2 => 6542925762,
  3 => 6623113406,
  4 => 6702782948,
)
Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96
3
$array = array($array[0]) + array_map('intval', $array);

(if you want to avoid a foreach loop)

nice ass
  • 16,471
  • 7
  • 50
  • 89
3
$arr = array_merge(array($arr[0]),array_map('intval', array_slice($arr, 1)));

Output:

array(5) {
  [0]=>
  string(2) "id"
  [1]=>
  int(2147483647)
  [2]=>
  int(2147483647)
  [3]=>
  int(2147483647)
  [4]=>
  int(2147483647)
}

Demo.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

php is loosely typed, you don't need to type cast it. It will do it for you. But if you want to explicitly do this, you can do like this:

$c = count($array)-1;
for ($n=1;$n<$c;$n++) {
  $array[$n] = (int) $array[$n];
}
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
0
foreach($array as $key => $val){
    if($key !== 0){
        $array[$key] = (int) $val;
    }
}

or

foreach($array as $key => $val){
    if(is_numeric($val)){
        $array[$key] = (int) $val;
    }
}
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
0

If all you values are int :

<?php
$arr = array(
  [0] => "id"
  [1] => "6086220176"
  [2] => "6542925762"
  [3] => "6623113406"
  [4] => "6702782948"
);

$arr = array_map(function($var) {
    // Your 'id' is not an int and will not be converted.
    return is_numeric($var) ? (int)$var : $var;
}, $arr);
grena
  • 1,011
  • 1
  • 10
  • 25
0
foreach($array as $k => $v){
  if($k == 0) continue;
  $array[$k] = is_int($v) ? intval($v) : $v; // if it's not convertible to int it keeps the value
}

or if you have non-numeric indexes

$skipped = false;
foreach($array as $k => $v){
  if($skipped === false){
    $skipped = true;
    continue;
  }
  $array[$k] = is_int($v) ? intval($v) : $v;
}
aleation
  • 4,796
  • 1
  • 21
  • 35
0

Another technique not yet mentioned on this page is to encode the array as a json string with the JSON_NUMERIC_CHECK flag so that numeric values are appropriately cast as integers or floats. Then decode the generated string back to an array.

This approach does not require the developer to know in advance the key/index of the value nor require a conditional check on each value to see if the value is numeric. In simple terms, it is highly portable as a general-use strategy.

This technique conveniently accommodates arrays with more than one level.

Code: (Demo)

$array = [
  "id",
  "6086220176",
  "6542925762",
  "6623113406",
  "6702782948",
  ['333', [['444']]]
];

var_export(
    json_decode(
        json_encode(
            $array,
            JSON_NUMERIC_CHECK
        ),
        true
    )
);

Output:

array (
  0 => 'id',
  1 => 6086220176,
  2 => 6542925762,
  3 => 6623113406,
  4 => 6702782948,
  5 => 
  array (
    0 => 333,
    1 => 
    array (
      0 => 
      array (
        0 => 444,
      ),
    ),
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-1

It's not clever to do this but this could probably works:

$narray = array();
foreach($array as $k => $v){
   $narray[$k+1] = $v;
}
$array = $narray;

$array is your array you want to start with 1.

idmean
  • 14,540
  • 9
  • 54
  • 83