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条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了