duandou1903 2011-05-23 22:55
浏览 34

使用搜索条件对多维数组进行排序

I am working with FLAT FILE database.

OK. Just to eliminate all the "why not use SQL". i HAVE. This is a personal(learning) project. I like working with text files and want to tinker.

So for 2+ days I've searched google and S.O. and tried many variations of methods presented. However they do not get what I am needing. But I did learn the right question to ask :-)

Now< in SQL you can do it this way (since sql is basically a fancy array)

SELECT * FROM [tablename] WHERE `some_field`='user_input1' ORDER BY `sequence_opt`;

I want to do that in a database of over 2000 records. (they have been serialized for ease of storing in a text file)

currently I did this: ($data_lib is 2000+ records with 30 fields each, but retrieved as

$data_lib[1]['a_field_name1']
$data_lib[2]['a_field_name2']
$data_lib[3]['a_field_name3']
$data_lib[4]['a_field_name etc...'])


 foreach($data_lib as $linenum=>$row)
            $sort[$linenum] = $row['some_field'];
            array_multisort($sort,SORT_DESC,$data_lib);

that is equivalent to SELECT * FROM [tablename] ORDER BY `some_field'

Works perfect. What I can't seem to do it get this to "WHERE" bY selecting another field as a criteria. As in the first SELECT example. I can't get the "WHERE" clause to work.

Now one thing I have not looked at, is the original way I am reading the database - do you think the issue is there? I have tried usort, but I seem to mess the fields doing it that way and the order is wrong.

This site has been absolutely wonderful with all its info, and my questions are perfectly answered.

thank you in advance!!!

  • 写回答

1条回答 默认 最新

  • douju8782 2011-05-23 23:03
    关注

    Something like this should do:

    $results = array_filter($records, function ($row) {
         return $row['some_field'] == 'user input';
    });
    usort($results, function ($a, $b) {
         return $a['sequence_opt'] - $b['sequence_opt'];  // assuming numeric field
    
         // alternatively something like:
         // return $a['sequence_opt'] == $b['sequence_opt'] ? 0 :
         //        ($a['sequence_opt'] < $b['sequence_opt'] ? -1 : 1);
    });
    

    It's really not very efficient though, compared to SQL.

    If this still messes up for you, please specify some more detailed input and expected output.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助