• php 数组查询


    php 数组查询

    <?php
    function arr_search ( $array , $expression ) {
       
    $result = array();
       
    $expression = preg_replace ( "/([^/s]+?)(=|<|>|!)/" , "/$a['$1']$2" , $expression );
        foreach (
    $array as $a ) if ( eval ( "return $expression;" ) ) $result [] = $a ;
        return
    $result ;
    }

    $data = array (
        array (
    "name" => "bill" , "age" => 40 ),
        array (
    "name" => "john" , "age" => 30 ),
        array (
    "name" => "jack" , "age" => 50 ),
        array (
    "name" => "john" , "age" => 25 )
    );

    print_r ( arr_search ( $data , "age>=30" ) );
    print_r ( arr_search ( $data , "name=='john'" ) );
    print_r ( arr_search ( $data , "age>25 and name=='john'" ) );
    ?>

    -- results --

    Array
    (
        [0] => Array
            (
                [name] => bill
                [age] => 40
            )

        [1] => Array
            (
                [name] => john
                [age] => 30
            )

        [2] => Array
            (
                [name] => jack
                [age] => 50
            )

    )
    Array
    (
        [0] => Array
            (
                [name] => john
                [age] => 30
            )

        [1] => Array
            (
                [name] => john
                [age] => 25
            )

    )
    Array
    (
        [0] => Array
            (
                [name] => john
                [age] => 30
            )

    )

  • 相关阅读:
    ajax01
    django04
    数据库
    WeakHashMap类
    IdentityHashMap
    Hashtable类
    LinkedHashMap类
    HashMap和TreeMap类
    PriorityQueue
    Synchronized
  • 原文地址:https://www.cnblogs.com/fengju/p/6173859.html
Copyright © 2020-2023  润新知