duanke2012 2013-02-13 14:30
浏览 47
已采纳

搜索php关联数组(哈希映射)作为mysql

example

public $inputs=array(
    array(  'sysname'=>'pt_name','dbname' => 'users.name','label' => 'user (name/ID)','value' => '',
            'type' => 'text','rules' => 'required','attr'=>'class="autocomplete"'),

    array(  'sysname'=>'pt_dob','dbname' => 'users.dob','label' => 'Patient Dob','value' => '',
            'type' => 'text','rules' => 'required','attr'=>'class="dob ac" Disabled'),

    array(  'sysname'=>'pt_gender','dbname' => 'users.gender','label' => 'gender','value' => 'male,female',
            'type' => 'dropdown','rules' => 'required','attr'=>'class="ac" Disabled'),

    array(  'sysname'=>'visit_date','dbname' => 'visits.date','label' => 'Date','value' => '',
            'type' => 'text','rules' => 'required','attr'=>'class="datepicker"'),

    array(  'sysname'=>'visit_time','dbname' => 'visits.time_booked','label' => 'Time','value' => '',
            'type' => 'text','rules' => 'required','attr'=>'class="timepicker"'),

    array(  'sysname'=>'visit_type','dbname' => 'visits.type','label' => 'Visit type','value' => 'visit,schedule',
            'type' => 'dropdown','rules' => 'required','attr'=>'')
    );

how can i search this array for only arrays that have pt_ in its sysname for example ?

the idea is i have many types of rows all in same table, so instead of running a mysql query to fetch each type separatly example:

$pt=db->query("select * from table where sysname like 'pt_%'")->result();
$visit=db->query("select * from table where sysname like 'visit_%'")->result();

i want to fetch all at one and split them in php to decrease db load.

so how can i do this ? and is it worth it or better of keep my querys separate.

  • 写回答

2条回答 默认 最新

  • douhuang1852 2013-02-13 14:52
    关注

    array_filter and a PHP-style closure* would be a pretty simple solution to this:

    function buildFilter($key, $needle) {
        return function($array) use($key, $needle) {
            return (strpos($array[$key], $needle) !== FALSE);
        };
    }
    $matches = array_filter($inputs, buildFilter('sysname', 'pt_'));
    var_dump($matches);
    
    • NB: What PHP calls a "closure" is quite a bit different from what most other languages use for the same term, so please make sure to read the PHP documentation.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献