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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?