Home  >  Article  >  Backend Development  >  PHP 处置多重数组

PHP 处置多重数组

WBOY
WBOYOriginal
2016-06-13 10:35:16830browse

PHP 处理多重数组


作者:林子木 wolinxuebin

博客地址:http://my.csdn.net/wolinxuebin

转载请保留

          今天在写PHP代码,要给手机端传送数据,数据是从mysql中取出来的。其中有个ID编号,mysql中存的是int型,取出来发现都成了string。

          在网上查了下,没有现成的解决方法,都仅仅说php是弱类型,没关系。可是,对方说,我要的是int型呀,没办法只能自己动手写了。

function array_intval(&$ar,$key,$base=10)	{	  foreach($ar as $k=>$v) {       		if(is_array($v)){    //判断是否是数组,一直将其打开    			 array_intval($ar[$k],$key,$base);  			}  			else {  				return $ar[$key] = intval($ar[$key],$base);  			}	  }	}
直接上代码。

array_intval($result, 'id');
调用直接这样写好了,因为传值进去是地址,所以不用再返回了。

其他处理也类似的,这可以说是一个模板。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn