common_helper 雨点打透心脏的1/2处 2022-03-15 01:52 168阅读 0赞 **1、将多维数组的数据单元的索引修改为某一列值** /** * @param $arrs * @param $name 字符串索引 * @return array */ function array_toolkit_index($arrs, $name) { if (empty($arrs)) { return array(); } $indexArray = array(); foreach ($arrs as $arr) { if (isset($arr[$name])) { $indexArray[$arr[$name]] = $arr; } } return $indexArray; } $records = array( array( 'id' => 2135 , 'first_name' => 'John' , 'last_name' => 'Doe' , ), array( 'id' => 3245 , 'first_name' => 'Sally' , 'last_name' => 'Smith' , ), array( 'id' => 5342 , 'first_name' => 'Jane' , 'last_name' => 'Jones' , ), array( 'id' => 5623 , 'first_name' => 'Peter' , 'last_name' => 'Doe' , ) ); $indexArray = array_toolkit_index($records, 'first_name'); echo '<pre>'; print_r($indexArray); /* * Array ( [John] => Array ( [id] => 2135 [first_name] => John [last_name] => Doe ) [Sally] => Array ( [id] => 3245 [first_name] => Sally [last_name] => Smith ) [Jane] => Array ( [id] => 5342 [first_name] => Jane [last_name] => Jones ) [Peter] => Array ( [id] => 5623 [first_name] => Peter [last_name] => Doe ) ) * */ **2、如果存在该键名,返回对应值,否则使用默认值** function get_param($params, $key, $defaultValue = '') { if (isset($params[$key])) { return $params[$key]; } return $defaultValue; } $where = get_param($ary,"where",[]); $select = get_param($ary,"select",[]); **3、 按某个键名对应的值进行分组** /** * 通过type类型对数据分组 * @param $arrs * @param $type * @return array */ function array_toolkit_group($arrs, $type) { if (empty($arrs)) { return array(); } $group = array(); foreach ($arrs as $arr) { if (empty($group[$arr[$type]])) { $group[$arr[$type]] = array(); } $group[$arr[$type]][] = $arr; } return $group; } //比如财务报表里,按日期进行分组 $products_sta = Sys_Product_Statistical_V3::get_rows([ 'where' => ['date >=' => $params['time'][0], 'date <=' => $params['time'][1]] ]); $products_sta = array_toolkit_group($products_sta, 'date'); /* Array ( [2019-03-19] => Array ( [0] => Array ( [id] => 2366 [product_id] => 2 [product_name] => 爱奇艺 [product_uv] => 1 [product_uv_source] => 漂流侠测试APP [product_settle_type] => UV [date] => 2019-03-19 ) [1] => Array ( [id] => 2367 [product_id] => 3 [product_name] => 新浪 [product_uv] => 1 [product_uv_source] => 漂流侠测试APP [product_settle_type] => CPA [date] => 2019-03-19 */ **4、将前端传过来的参数进行处理,为空的参数去掉** //php7不支持each()函数了,改为第二种 function array_remove_empty_string($arr, $allow = []) { $newArr = array(); while(list($key, $val) = each($arr)) { if (is_array($val)){ $val = array_remove_empty_string($val, $allow); if (count($val)!= 0) { $newArr[$key] = $val; } } elseif (trim($val) != "" || in_array($key, $allow)) { $newArr[$key] = $val; } } unset($arr); return $newArr; } function array_remove_empty_string($arr, $allow = []) { $newArr = array(); array_walk($arr, function($value, $key, $allow) use (&$newArr){ if (is_array($value)){ $value = array_remove_empty_string($value, $allow); if (count($value)!= 0) { $newArr[$key] = $value; } }elseif (trim($value) != "" || in_array($key, $allow)) { $newArr[$key] = $value; } }, $allow); unset($arr); return $newArr; } $params = array_remove_empty_string($params); /* Array ( [where] => Array ( [date] => 2019-03-21 ) [like] => Array ( [product_name] => [product_uv_source] => ) [order_by] => total_valid_uv DESC [limit] => Array ( [0] => 30 [1] => 0 ) ) 处理后 Array ( [where] => Array ( [date] => 2019-03-21 ) [order_by] => total_valid_uv DESC [limit] => Array ( [0] => 30 [1] => 0 ) ) */
还没有评论,来说两句吧...