Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...