Today, when I was writing an interface to synchronize data with a third party, I had a big piece of data, but the third party needed only a few of them, and it started out as much as I thought.
$ret Array (); foreach ($needParamsas$pk) { if (isset($params[ $PK ] ) {$ret[$pk$params[$pk]; }}
Due to the need to deal with some special parameters, looking very uncomfortable
Think of the PHP array function is very powerful, and then changed to
$needkey Array (' uid ', ' cuid '); $params Array (' uid ' = = 123, ' cuid ' + 1234567, ' other ' = ' ... '); $ret = Array_intersect_key ($paramsarray_flip($needkey));
Change into
/** * _packparams * * @desc extract pass parameters from public parameters according to configuration*/ Private function_packparams ($params,$needKey) { $ret= Array_intersect_key ($params,$needKey); //special handling of individual parameters if(isset($ret[' UID '])) { $ret[' cuid '] =$ret[' UID ']; unset($ret[' UID ']); } if(isset($needKey[' Puid '])) { $PASSSRV=$this->getinstance (' Util_passport '); $uid=$PASSSRV->getuidfromcookie ($params[' Cookie ']); $ret[' uid '] =NULL===$uid? ‘‘ :$uid; } return $ret; }
php-array functions