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 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决