Convert numeric strings in multi-dimensional array to int

我是研究僧i 提交于 2020-12-16 05:20:52

问题


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.


回答1:


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



回答2:


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



回答3:


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



回答4:


You can use the intval function to translate strings into integers:

$integervalue = intval($stringvalue);



回答5:


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



回答6:


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



回答7:


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



回答8:


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.



来源:https://stackoverflow.com/questions/27147105/convert-numeric-strings-in-multi-dimensional-array-to-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!