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 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络