php array_merge($a,$b)与 $a+$b区别
array_merge 数字键名会被重新编号,what's '...'
$data = [[1, 2], [3], [4, 5]]; var_dump($data); // [[1, 2], [3], [4, 5]]; var_dump(... $data); // [1, 2, [3], [4, 5]]; var_dump(array_merge($data)); //[[1, 2], [3], [4, 5]]; var_dump(array_merge(... $data)); // [1, 2, 3, 4, 5];
php嵌套
<?php function a(){ echo 'a'; function b(){ echo 'b'; function c(){ echo 'c'; } } }
// a();c(); //a
a(); b(); c(); //abc
必须先执行外部函数才能依次调用,直接调用b无返回结果;
<?php if (!function_exists('a')) { function a(){ echo 'in a'; } } function a(){ echo 'out a'; } a(); //'out a'
<?php if (!function_exists('a')) { function a(){ echo 'in a'; } } a(); //'in a'
<?php return ['a'=>'b'];
function a($a){ var_dump($a); } a(include ./a.php); //['a'=>'b']
PHP数组相关:
$a = ['a' => 'b', 'c' => 'd']; $a[] = ['e'=>'f']; echo '<pre>'; var_dump($a);
//需求解析url,类似与bash
/** * @param $url string "ab/{cd,ef,gh}/{ij,kl}" * @return array ['abcdij','abcdkl','abefij','abefkl','abghij','abghkl'] */
//总觉得有还有很大的优化空间 2017-12-20 13:47:17
function preUrl($url = "01/ab/{cd,ef,gh}/{ij,kl}") { static $result = []; $pattern = '/{(\w+,)*(\w+)}/'; if (preg_match($pattern, $url, $matches)) { $head = strstr($url, $matches[0], true); $tail = substr(strstr($url, $matches[0]), strlen($matches[0])); foreach (explode(',', rtrim(ltrim($matches[0], '{'), '}')) as $value) { $tempUrl = $head . $value . $tail; if (preg_match($pattern, $tempUrl, $matches)) { preUrl($tempUrl); } else { $result[] = $tempUrl; } } } else { $result = [$url]; } return $result; }
处理字符串想通过换行符来分割成数组
explode('\r\n', $a); //错误 explode("\r\n", $a); //正确
php删除目录下所有文件和文件夹
public function removePrizeData($attachPath = null) { $prizeDataPath = is_null($attachPath) ? Env::get('ROOT_PATH') . self::PRIZE_FILE_PATH : $attachPath; if ($handle = opendir($prizeDataPath)) { while (false !== ($file = readdir($handle))) { if ($file == '.' || $file == '..') continue; if (is_dir($prizeDataPath . '/' . $file)) { $this->removePrizeData($prizeDataPath . '/' . $file); rmdir($prizeDataPath . '/' . $file); } else { unlink($prizeDataPath . '/' . $file); } } closedir($handle); } }
类似explode函数,但可以指定返回类型 2018-02-28 16:51:22
/** * foreach(explodeType(',', '1,2,3', 'int', [2]) as $value) dump($value); * output: int(1) * int(3) * @param string $delimiter 切割符号 * @param string $string 被切割字符串 * @param string $valueType 返回的结果类型 * @param array $filter 过滤器,想要清洗的值,可以修改成闭包让功能更多 * @return \Generator * @author GP 20180228 */ static public function explodeType(string $delimiter, string $string, string $valueType = 'int', array $filter = []) { $loop = function () use ($string, $delimiter) { static $begin = true; if ($begin) { $begin = false; return strtok($string, $delimiter); } else { return strtok($delimiter); } }; $filter = function ($result) use ($filter) { return in_array($result, $filter) ? true : false; }; while (false !== $temp = $loop()) { switch ($valueType) { case 'int': $i = (int)$temp; if ($filter && true === $filter($i)) continue; yield $i; } } }
发现了一个php语法,可以对null取元素,结果也是null
var_dump(null['a'][0]['b']); ===>>> null