zoukankan      html  css  js  c++  java
  • PHP中create_function的用法总结

    在php中,函数create_function主要用来创建匿名函数,有时候匿名函数可以发挥它的作用。

    1.测试一

    测试一主要用来循环替换数组中多个值的<与>,我们用array_map加上create_function解决这个问题。

    ###############################################

    function filterChars($a) {

    if (!is_array($a)) return false;

    $a = array_map(create_function(‘$b’, ‘return str_replace(array(“>”, “<”), array(“&gt;”, “&lt;”), $b);’), $a);

    return $a;

    }

    $a = array(‘abcd&>’, ‘def<’);

    echo “array_map and create_function ”;

    print_r(filterChars($a));

    ###############################################

    2.测试二

    测试二与测试一实现同样的功能,只不过我们用的不是匿名函数,而是用自定义函数实现的,而且这个自定义函数实现在另外一个函数里面。

    ###############################################

    function filterChars2($a) {

    if (!is_array($a)) return false;

    function filterStr($str) {

    return str_replace(array(“>”, “<”), array(“&gt;”, “&lt;”), $str);

    }

    $a = array_map(‘filterStr’, $a);

    return $a;

    }

    $a = array(‘abcd&>’, ‘def<’);

    echo “array_map and create_function and custom function ”;

    print_r(filterChars2($a));

    //如果调用了filterchar2函数,则在下面调用filterStr时不报错,如果在上面没有调用filterChar2函数,则会报类似如下的错误,Fatal error: Call to undefined function filterStr() in D:myphp esearch est4.php on line 334

    echo ‘filterStr:’ . filterStr(‘ab&<>cd’) . “ ”;

    ###############################################

    3.测试三

    测试三主要用来过滤文件中的空行,同时用trim去掉每一行的换行符然后保存在数组中。

    ###############################################

    $file = ‘tmp_user.txt’;

    if (file_exists($file)) {

    $usernames = file($file);

    print_r($usernames);

    /*

    array array_filter ( array input [, callback callback] )

    array_filter() 依次将 input 数组中的每个值传递到 callback 函数。如果 callback 函数返回 TRUE,则 input 数组的当前值会被包含在返回的结果数组中。数组的键名保留不变。

    切记,即使在array_filter中的create_function中用trim过滤了空白字符,但是仍然需要用array_map处理每一行中的内容,因为array_filter的作用是,只要回调函数返回true,则数组中当前的值被返回,返回的是用trim处理之前的值,也就是原始的值。

    */

    $usernames = array_filter($usernames, create_function(‘$id’, ‘$id = trim($id); return !empty($id);’));

    //由于文件通过file加载到数组中,因此一定要用trim处理一下去掉换行符

    $usernames = array_map(create_function(‘$id’, ‘$id = trim($id); return $id;’), $usernames);

    print_r($usernames);

    }

    ###############################################

    输出如下:

    测试一

    array_map and create_function

    Array

    (

    [0] => abcd&&gt;

    [1] => def&lt;

    )

    测试二

    array_map and create_function and custom function

    Array

    (

    [0] => abcd&&gt;

    [1] => def&lt;

    )

    filterStr:ab&&lt;&gt;cd

    测试三

    Array

    (

    [0] => ab

    [1] => cd

    [2] =>

    [3] => ef

    [4] => gh

    )

    Array

    (

    [0] => ab

    [1] => cd

    [3] => ef

    [4] => gh

    )

  • 相关阅读:
    一个把数据转化成Excel导出的程序 python Django
    Ubuntu常用命令
    Git 常用命令 和 安装
    strick-footer 粘边布局
    Django
    CSS基础 和 font字体、背景属性连写 与 清除浮动方法
    MySQL 40题练习题和答案
    JS(ES6)、Vue.js、node.js
    ORM框架 和 面向对象编程
    MySQL基本指令3 和 索引 、分页
  • 原文地址:https://www.cnblogs.com/chengzhi59/p/7101978.html
Copyright © 2011-2022 走看看