-1

I have a multi-dimensional array:

array(3) {
    [0]=> array(2) {
        [0]=> string(3) "ABC"
        [1]=> string(3) "744"
    }
    [1]=> array(2) {
        [0]=> string(3) "DEF" 
        [1]=> string(2) "86"
    }
    [2]=> array(2) {
        [0]=> string(3) "GHI" 
        [1]=> string(1) "2"
    }
} 

Now I want to convert the string-type values which are numbers to integer type.

Ex 744 is string but it should be integer.

I have tried a few functions after searching forums but with no success. Later I have to use this array in json_encode() function and in my javascript process numeric values must be integer-type.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
neophyte
  • 1,726
  • 3
  • 15
  • 21
  • In JavaScript you can just use parseInt(object.variable); - In PHP you can foreach($array as &$var) by reference WITH the `&` through the array use if(is_numeric($var)) then cast the variable as an int $var = (int) $var; – Sunhat Nov 26 '14 at 10:56
  • For researchers looking for a solution that is suited to flat array data, please see this similar question: https://stackoverflow.com/q/9593765/2943403 – mickmackusa Oct 14 '20 at 03:02

7 Answers7

1

You can use intval, like the other answers suggested, in conjunction with array_walk_recursive:

array_walk_recursive($array, function(&$item) {
    if (is_numeric($item)) {
        $item = intval($item);
    }
});
Daniel Ribeiro
  • 10,156
  • 12
  • 47
  • 79
1

You can implement a recursive function as follows, which will take an array of any size and dimension.

 function convertArray(myArray){
    convertedArray=array();
    foreach(myArray as key=>value){
        if(is_array(value)){
            convertedArray[key] = convertArray(value);      
        }else{
            temp = intval(value);
            if(temp==0){
                convertedArray[key] = value;
            }else{
                convertedArray[key] = temp;
            }       
        }
    }
    return convertedArray;
 }
Simon Savai
  • 182
  • 2
  • 13
  • **If** the input data has variable depth/structure, [Daniel Ribeiro](https://stackoverflow.com/a/27147253/2943403)'s snippet that employs `array_walk_recursive()` is a less laborious snippet and easier to read. – mickmackusa Jan 20 '21 at 00:26
1

Later I have to use this array in json_encode() function and in my javascript process numeric values must be integer-type.

Have you checked the JSON Predefined Constants:

$result = json_encode($array, JSON_NUMERIC_CHECK);

JSON_NUMERIC_CHECK (int)

Encodes numeric strings as numbers. Available as of PHP 5.3.3.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • This answer cuts to the heart of the matter, instead of focussing on the XY Problem. This answer informs the OP to avoid performing iterated preparations (the long way around). It is most professional and direct to use this solution if you are converting your data to a json string. – mickmackusa Jan 20 '21 at 00:14
0

just use intval(string);

ref: http://php.net/manual/en/function.intval.php

EDIT:

If you will convert the whole array it wont work. Then you have to test the non-int values so it wont be converted.

inval($value) will return 0 for non-int value then you have to test if the value was 0 or just non-int value.

If you always have the int value as a second index in array then just use intval($value);

foreach ($a as &$ar) {
  $ar[1] = intval($ar[1]);
} 
tttpapi
  • 887
  • 2
  • 9
  • 32
0

try this

$a = array( 0=>  array( 0=> "ABC" ,1=> "744" ) ,1=>  array( 0=>  "DEF",1=>  "86" ),  2=>    array( 0=> "GHI", 1=> "2" ) );
foreach ($a as $i)
echo intval($i[1]);
user3510665
  • 218
  • 3
  • 13
  • This answer is not very flexible and it only echos instead of overwriting the subarray values. This answer is also "code-only" which means that it will be low-value to researchers. This answer can be safely removed because tttpapi's answer more appropriately uses this technique (and was posted earlier). – mickmackusa Jan 20 '21 at 00:19
0

if you need to check if value can be converted to int - see this question after it just use foreach for loop array and if current iteration value can be converted to int - use intval.

Example -

$a = array('test', '11', 'abc', '14554');
echo "<pre>";
var_dump($a);
echo "</pre>";
foreach ($a as &$item) {
if (ctype_digit($item)) {
    $item = intval($item);
}
}
echo "<pre>";
var_dump($a);
echo "</pre>";
Community
  • 1
  • 1
IQW
  • 829
  • 1
  • 9
  • 11
  • A note for researchers, if you have negative numeric strings, `ctype_digit()` will not be satisfied because it requires all characters in the string to be digits -- thus negative numbers will not be cast as negative integers. This answer is necessarily _wrong_, I just wanted to express a caveat to this technique. – mickmackusa Jan 20 '21 at 00:29
0
$variable = array(3) { 
        [0]=> array(2) { 
                        [0]=> string(3) "ABC" 
                        [1]=> string(3) "744" } 
        [1]=> array(2) { 
                         [0]=> string(3) "DEF" 
                         [1]=> string(2) "86" } 
        [2]=> array(2) { 
                         [0]=> string(3) "GHI" 
                         [1]=> string(1) "2"  }
    } 

foreach($variable as $key=>$arrayStrings)
    foreach($arrayStrings as $innerKey=>$string)
           if(is_numeric($string))
              $variable[$key][$innerKey] = intval($string);
gbvisconti
  • 412
  • 1
  • 4
  • 19