ThinkPHP Êý×éÌæ»»ÖÜÈ«ÆÊÎö
ÔÚ±àд php ³ÌÐòʱ£¬´¦Öóͷ£Êý×éÊÇÒ»¸ö³£¼ûµÄʹÃü¡£ÔÚ thinkphp ÏîÄ¿ÖУ¬¾³£ÐèÒª¶ÔÊý×éÖеÄijЩֵ¾ÙÐÐÌæ»»£¬ÀýÈ罫Êý×éÖÐËùÓеĿÕ×Ö·û´®£¨”£©Ì滻Ϊ null¡£±¾ÎĽ«Îª¸÷ÈËÏÈÈÝ thinkphp ÖеĶàÖÖÊý×éÌæ»»ÒªÁì¡£
Ò»¡¢Í¨Ë×Êý×éÌæ»»
Ê×ÏÈ£¬ÎÒÃÇÀ´¿´Ò»ÏÂͨË×Êý×éµÄÌæ»»ÒªÁì¡£¼ÙÉèÎÒÃÇÓÐÒ»¸öÊý×é $arr£¬ÆäÖаüÀ¨ËĸöÔªËØ£º
$arr = array(
'name' => 'ÕÅÈý', 'age' => 18, 'email' => 'zhangsan@example.com', 'gender' => 'ÄÐ',
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
);
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÈôÊÇÎÒÃÇÏ뽫Êý×éÖÐËùÓеĿÕ×Ö·û´®Ì滻Ϊ null£¬¿ÉÒÔʹÓà foreach Ñ»·±éÀúÊý×飬¶Ôÿ¸öÔªËؾÙÐÐÌæ»»¡£´úÂëÈçÏ£º
foreach ($arr as &$value) {
if ($value === '') { $value = null; }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
}
ÆäÖУ¬&$value ÌåÏÖ¶ÔÊý×éÔªËصÄÒýÓã¬ÕâÑù¾Í¿ÉÒÔÖ±½ÓÐÞ¸ÄÔÊý×é¡£ÔËÐÐÉÏÊö´úÂëºó£¬$arr µÄÖµ»áÄð³É£º
array(
'name' => 'ÕÅÈý', 'age' => 18, 'email' => 'zhangsan@example.com', 'gender' => 'ÄÐ',
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
);
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÈôÊÇÏëÒªÌæ»»Ö¸¶¨¼üÃûµÄÖµ£¬¿ÉÒÔÔÚÑ»·ÖÐÌí¼ÓÌõ¼þÅжϣ¬´úÂëÈçÏ£º
foreach ($arr as $key => &$value) {
if ($key === 'email' && $value === 'zhangsan@example.com') { $value = 'lisi@example.com'; }
µÇ¼ºó¸´ÖÆ
}
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÕë¶Ô email ¼üÃûµÄÖµ¾ÙÐÐÁËÌæ»»£¬¼´½« ‘zhangsan@example.com’ Ì滻Ϊ ‘lisi@example.com’£¬ÈôÊÇÔËÐиôúÂ룬$arr µÄÖµ½«±äΪ£º
array(
'name' => 'ÕÅÈý', 'age' => 18, 'email' => 'lisi@example.com', 'gender' => 'ÄÐ',
µÇ¼ºó¸´ÖÆ
);
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
µ«ÕâÖÖÒªÁìÓÐÒ»¸öÈõµã£¬ÈôÊÇÊý×é½Ï´ó£¬Ê¹ÓÃÑ»·¾ÙÐбéÀúÌæ»»µÄЧÂÊ»áºÜµÍ¡£ÒÔÊÇÎÒÃÇÐèÒªÕÒµ½Ô½·¢¸ßЧµÄÌæ»»ÒªÁì¡£
¶þ¡¢µÝ¹éÊý×éÌæ»»
ÈôÊÇÎÒÃÇÐèÒª¶ÔÒ»¸ö¶àάÊý×éÖеÄËùÓпÕ×Ö·û´®Ì滻Ϊ null£¬ÄÇôʹÓÃÑ»·ÒѾÎÞ·¨Ê¤ÈÎÁË¡£Õâʱ£¬ÎÒÃÇ¿ÉÒÔʹÓõݹéµÄÒªÁ죬±éÀúÕû¸öÊý×飬¶Ôÿ¸ö×ÓÊý×éÔÙ¾ÙÐеݹ飬ֱµ½ÕÒµ½×î»ù±¾µÄÔªËؾÙÐÐÌæ»»¡£
´úÂëÈçÏ£º
function array_replace_recursive_null($arr) {
foreach ($arr as $key => &$value) { if (is_array($value)) { $value = array_replace_recursive_null($value); } elseif ($value === '') { $value = null; } } return $arr;
µÇ¼ºó¸´ÖÆ
}
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏÈÅжÏÄ¿½ñÔªËØÊÇ·ñÊÇÊý×飬ÈôÊÇÊÇ£¬ÔòµÝ¹éŲÓú¯Êý array_replace_recursive_null() ¾ÙÐÐÌæ»»£¬ÈôÊDz»ÊÇ£¬ÔòÅжÏÄ¿½ñÔªËØÊÇ·ñΪ¿Õ×Ö·û´®£¬ÈôÊÇÊÇ£¬Ôò½«ÆäÌ滻Ϊ null¡£
ʹÓøú¯Êý¶ÔÊý×é¾ÙÐÐÌæ»»£¬´úÂëÈçÏ£º
$arr = array(
'name' => 'ÕÅÈý', 'age' => 18, 'contact' => array( 'email' => 'zhangsan@example.com', 'phone' => '', 'address' => array( 'province' => '¹ã¶«Ê¡', 'city' => '', 'district' => '·¬Ø®Çø', ), ),
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
);
$arr = array_replace_recursive_null($arr);
ÈôÊÇÔËÐÐÉÏÊö´úÂ룬$arr µÄÖµ½«±äΪ£º
array(
'name' => 'ÕÅÈý', 'age' => 18, 'contact' => array( 'email' => 'zhangsan@example.com', 'phone' => null, 'address' => array( 'province' => '¹ã¶«Ê¡', 'city' => null, 'district' => '·¬Ø®Çø', ), ),
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
);
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
µÝ¹éÊý×éÌæ»»ÊÊÓÃÓÚÕû¸ö¶àάÊý×éµÄÌæ»»£¬µ«Õë¶Ôµ¥¸öÖµµÄÌæ»»ÈÔÈ»ÐèҪʹÓÃÑ»·±éÀú¡£
Èý¡¢Ê¹Óà array_map() º¯ÊýÌæ»»
PHP ÖÐµÄ array_map() º¯Êý¿ÉÒÔ¶ÔÊý×éÖеÄÿ¸öÔªËØÓ¦ÓÃÖ¸¶¨µÄ»Øµ÷º¯Êý£¬·µ»ØÒ»¸öеÄÊý×é¡£ÎÒÃÇ¿ÉÒÔʹÓøú¯ÊýÀ´¶ÔÊý×éÖеÄÖµ¾ÙÐÐÌæ»»¡£
¼ÙÉèÎÒÃÇÓÐÒ»¸ö°üÀ¨ÒÔÏÂÊý¾ÝµÄÊý×飺
$arr = array(‘apple’, ‘orange’, ‘banana’, ”);
ÎÒÃÇÏë°ÑÆäÖеĿÕ×Ö·û´®Ìæ»»³É null£¬¿ÉÒÔʹÓà array_map() º¯ÊýºÍÒ»¸ö»Øµ÷º¯ÊýÀ´ÊµÏÖ¡£
´úÂëÈçÏ£º
function replace_null($value) {
return $value === '' ? null : $value;
µÇ¼ºó¸´ÖÆ
}
$arr = array_map(‘replace_null’, $arr);
ÔÚ´úÂëÖУ¬ÎÒÃDZàдÁËÒ»¸ö replace_null() »Øµ÷º¯Êý£¬ÈôÊÇ $value ÊÇÒ»¸ö¿Õ×Ö·û´®£¬¾Í·µ»Ø null£¬²»È»·µ»Ø $value¡£È»ºó½«¸Ãº¯Êý×÷Ϊ»Øµ÷º¯Êýת´ï¸ø array_map() º¯Êý£¬Íê³ÉÊý×éÖÐÖµµÄÌæ»»¡£
ËÄ¡¢Ê¹Óà array_walk_recursive() º¯ÊýÌæ»»
array_walk_recursive() º¯ÊýÓÃÓڵݹéµØÓ¦ÓÃÓû§×Ô½ç˵º¯Êý¸øÊý×é»ò¹¤¾ßµÄÿ¸öÔªËØ¡£Óë array_map() ÀàËÆ£¬µ«¿ÉÒԵݹé±éÀú¶àάÊý×é¡£ÎÒÃÇ¿ÉÒÔʹÓøú¯ÊýÀ´¶Ô¶àάÊý×éÖеÄËùÓÐÖµ¾ÙÐÐÌæ»»¡£
¼ÙÉèÎÒÃÇÓÐÒ»¸ö¶àάÊý×é $arr£¬ÆäÖаüÀ¨ÁËһЩ¿Õ×Ö·û´®£º
$arr = array(
'name' => 'ÕÅÈý', 'age' => 18, 'contact' => array( 'email' => 'zhangsan@example.com', 'phone' => '', 'address' => array( 'province' => '¹ã¶«Ê¡', 'city' => '', 'district' => '·¬Ø®Çø', ), ),
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
);
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÏÖÔÚ£¬ÎÒÃÇÒª°ÑÆäÖеĿÕ×Ö·û´®¶¼Ìæ»»³É null¡£¿ÉÒÔʹÓà array_walk_recursive() º¯ÊýºÍÒ»¸ö»Øµ÷º¯ÊýÀ´ÊµÏÖ¡£
´úÂëÈçÏ£º
function replace_null_recursive(&$value, $key) {
if ($value === '') { $value = null; }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
}
array_walk_recursive($arr, ‘replace_null_recursive’);
ÔÚ´úÂëÖУ¬ÎÒÃDZàдÁËÒ»¸ö replace_null_recursive() »Øµ÷º¯Êý£¬ÈôÊÇ $value ÊÇÒ»¸ö¿Õ×Ö·û´®£¬¾Í½«ÆäÌæ»»³É null£¬È»ºó½«¸Ãº¯Êý×÷Ϊ»Øµ÷º¯Êýת´ï¸ø array_walk_recursive() º¯Êý¡£
ÔËÐÐÉÏÊö´úÂëºó£¬$arr µÄÖµ½«ÈçÏÂËùʾ£º
array(
'name' => 'ÕÅÈý', 'age' => 18, 'contact' => array( 'email' => 'zhangsan@example.com', 'phone' => null, 'address' => array( 'province' => '¹ã¶«Ê¡', 'city' => null, 'district' => '·¬Ø®Çø', ), ),
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
);
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Îå¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔÚ ThinkPHP ÏîÄ¿ÖжÔÊý×é¾ÙÐÐÌæ»»µÄ¶àÖÖÒªÁ죬°üÀ¨Í¨Ë×Êý×éÌæ»»¡¢µÝ¹éÊý×éÌæ»»¡¢Ê¹Óà array_map() º¯ÊýÌæ»»ºÍʹÓà array_walk_recursive() º¯ÊýÌæ»»¡£Ã¿ÖÖÒªÁ춼ÓÐÊÊÓõij¡¾°£¬Æ¾Ö¤ÏÖÕæÏàÐÎÑ¡Ôñ×îºÏÊʵÄÒªÁì¿ÉÒÔ½ÚԼʱ¼äºÍ¾«Éñ¡£
×îºó£¬Ï£Íû±¾ÎÄÄܹ»¶Ô¶ÁÕßÓÐËù×ÊÖú£¬ÔÚ´¦Öóͷ£Êý×éÌ滻ʱÄܹ»Ô½·¢¸ßЧµØÍê³ÉʹÃü¡£
ÒÔÉϾÍÊÇThinkPHP Êý×éÌæ»»ÖÜÈ«ÆÊÎöµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡