Last active
October 19, 2016 03:20
-
-
Save sursir/7e14bee8142e05c331ea to your computer and use it in GitHub Desktop.
php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 从二维数组中选取指定列组成新数组 | |
* @param array $dd_array 二维数组 | |
* @param string | int $column_key 选取作为值的列的列名 index | key | |
* @param string | int $index_key 选取作为键的列的列明 | |
* @param string $flag 列名的形式 | |
* @return array 返回新的一维数组 | |
*/ | |
function array_column($dd_array, $column_key, $index_key = 0, $flag = 'INDEX') | |
{ | |
$flag = strtoupper($flag); | |
$new_array = array(); | |
$i = 0; | |
$key = ''; | |
$colKey = ''; | |
$colValue = ''; | |
// 继承上层数组 | |
if ($flag == 'INHERIT') $newKey = &$key; | |
// 索引为 指定列的值 | |
else if ($flag == 'COLUMN_VALUE') $newKey = &$colKey; | |
// 自由索引 | |
else $newKey = &$i; | |
// 不为空的时候返回键值数组 | |
if ($column_key !== false && $column_key !== null) { | |
$newValue = &$colValue; | |
} else { | |
$newValue = &$value; | |
} | |
foreach ($dd_array as $key => $value) { | |
$colKey = $value[$index_key]; | |
$colValue = $value[$column_key]; | |
$new_array[$newKey] = $newValue; | |
$i++; | |
} | |
return $new_array; | |
} | |
$dd = array( | |
'c111' => array('a' => 1, 'b' => 2, 'c' => 3, 4), | |
'c222' => array('a' => 10, 'b' => 20, 'c' => 30, 40), | |
'c333' => array('a' => 100, 'b' => 200, 'c' => 300, 400), | |
'c444' => array('a' => 1000, 'b' => 2000, 'c' => 3000, 4000), | |
'c555' => array('a' => 10000, 'b' => 20000, 'c' => 30000, 40000) | |
); | |
$ddd = array_column($dd, 'b', false, 'INHERIT'); | |
var_dump($ddd); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 字符串转数组,指定字符集 | |
* @param string $str 要转换的字符串 | |
* @param string $charset 字符集格式 | |
* @return array 转换后的数组 | |
*/ | |
function str2arr($str, $charset){ | |
$strlen = mb_strlen($str, $charset); | |
$strArray = array(); | |
$tag = $strlen; | |
while ($tag) { | |
$index = $strlen-$tag; | |
$strArray[] = mb_substr($str, $index, 1, $charset); | |
$tag--; | |
} | |
return $strArray; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Math.convert: | |
echo "Math :*******************\n"; | |
var_dump('=====', bindec('10101023')); | |
var_dump('=====', dechex(121212)); | |
// string: | |
// string 版 Math.convert | |
base_convert($number, $from, $to); | |
// 二进制数据与10进制互转 | |
chr($charDec); | |
ord($char); | |
// 16进制 多字符版 chr ord | |
$chars = hex2bin($charHexes); | |
$charHexes = bin2hex($chars); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 按长度分割一个中文字符串 | |
* @param string $str 要分割的中文字符串 | |
* @param integer $length 截取长度 | |
* @param boolean $limit 是否有长度限制 | |
* @return array 返回分割后的数组 | |
*/ | |
function zh_split($str, $length = 1, $limit = false) | |
{ | |
if ($length < 1) throw new Exception('lenght must more than 1'); | |
if ($limit < 0) throw new Exception('limit must more than 0 or be a false'); | |
$zhs = preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY); | |
$strChunks = array_chunk($zhs, $length); | |
// 如果limit大于 分割后结果大小 则跳过分割 | |
if ($limit < count($strChunks) || $limit < 1) { | |
$wholePiece = array_splice($strChunks, $limit); | |
$wholeStr = ''; | |
array_map(function($val) use (&$wholeStr) { | |
$wholeStr .= implode($val); | |
}, $wholePiece); | |
} | |
$strs = array_map(function($vals){ | |
return implode($vals); | |
}, $strChunks); | |
if ($limit < count($strChunks) || $limit < 1) { | |
return array_merge($strs, array($wholeStr)); | |
} else { | |
return $strs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment