-1
votes

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.

7
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
For researchers looking for a solution that is suited to flat array data, please see this similar question: stackoverflow.com/q/9593765/2943403 - mickmackusa

7 Answers

0
votes
$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);
1
votes

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;
 }
1
votes

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.

0
votes

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]);
} 
0
votes

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]);
0
votes

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>";
0
votes

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);
    }
});