将多维数组中的数字字符串转换为int [英] Convert numeric strings in multi-dimensional array to int
本文介绍了将多维数组中的数字字符串转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个多维数组:
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
是字符串,但应为整数.
Ex 744
is string but it should be integer.
在搜索论坛后,我尝试了一些功能,但没有成功.稍后,我必须在 json_encode()
函数中使用此数组,并且在我的javascript处理中,数字值必须为整数类型.
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.
推荐答案
$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);
这篇关于将多维数组中的数字字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文