dongqian1028 2017-11-27 03:16
浏览 512
已采纳

按第二个数组中的值过滤数组

** I have edited this to show how I got my code to work using array_search

I have an array, $arr1 with 5 columns as such:

 key    id  name    style   age whim
 0      14  bob     big     33  no
 1      72  jill    big     22  yes
 2      39  sue     yes     111 yes
 3      994 lucy    small   23  no
 4      15  sis     med     24  no
 5      16  maj     med     87  yes
 6      879 Ike     larg    56  no
 7      286 Jed     big     23  yes

This array is in a cache, not a database.

I then have a second array with a list of id values -

$arr2 = (0=>14, 1=>72, 2=>8790)

How do I filter $arr1 so it returns only the rows with the id values in $arr2?

I got my code to work as follows:

$arr1 = new CachedStuff();  // get cache

$resultingArray = [];  // create an empty array to hold rows
$filter_function = function ($row) use ($arr2) {
    return (array_search($row['id'], $arr2));
};
$resultingArrayIDs = $arr1->GetIds($filter_function, $resultingArray);

This gives me two outputs: $resultingArray & $resultingArrayIDs both of which represent the intersection of the $arr1 and $arr2.

  • 写回答

4条回答 默认 最新

  • duancheng6500 2017-11-27 04:41
    关注

    The trouble with using iterated checks on the second/filtering array is that it will lose efficiency as the arrays' sizes increase.

    By assigning new keys to the first array, and flipping the filtering array's values to keys, you run a one-time filter/intersect on the arrays and then reindex the output array.

    My method calls no foreach() loops and has no iterated conditionals.

    Code: (Demo)

    $arr1=[
        ['key'=>0,'id'=>14,'name'=>'bob','style'=>'big','age'=>33,'whim'=>'no'],
        ['key'=>1,'id'=>72,'name'=>'jill','style'=>'big','age'=>22,'whim'=>'yes'],
        ['key'=>2,'id'=>39,'name'=>'sue','style'=>'yes','age'=>111,'whim'=>'yes'],
        ['key'=>3,'id'=>994,'name'=>'lucy','style'=>'small','age'=>23,'whim'=>'no'],
        ['key'=>4,'id'=>15,'name'=>'sis','style'=>'med','age'=>24,'whim'=>'no'],
        ['key'=>5,'id'=>16,'name'=>'maj','style'=>'med','age'=>87,'whim'=>'yes'],
        ['key'=>6,'id'=>879,'name'=>'Ike','style'=>'larg','age'=>56,'whim'=>'no'],
        ['key'=>7,'id'=>286,'name'=>'Jed','style'=>'big','age'=>23,'whim'=>'yes']
    ];
    $arr2=[14,72,879];
    
    $arr1=array_column($arr1,null,'id');  // generate temporary keys
    //var_export($arr1);
    
    $arr2=array_flip($arr2);  // move values to keys
    //var_export($arr2);
    
    $filtered=array_intersect_key($arr1,$arr2);  // retain subarrays that qualify
    //var_export($filtered);
    
    $reindexed=array_values($filtered);  // reindex array
    var_export($reindexed);
    

    or written as a one-liner like this:

    var_export(array_values(array_intersect_key(array_column($arr1,null,'id'),array_flip($arr2))));
    

    Output:

    array (
      0 => 
      array (
        'key' => 0,
        'id' => 14,
        'name' => 'bob',
        'style' => 'big',
        'age' => 33,
        'whim' => 'no',
      ),
      1 => 
      array (
        'key' => 1,
        'id' => 72,
        'name' => 'jill',
        'style' => 'big',
        'age' => 22,
        'whim' => 'yes',
      ),
      2 => 
      array (
        'key' => 6,
        'id' => 879,
        'name' => 'Ike',
        'style' => 'larg',
        'age' => 56,
        'whim' => 'no',
      ),
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上