简体   繁体   English

如何在php中过滤键值匹配的数组

[英]how to filter array where a key's value matches in php

I'm not sure how to do this, but I would like to filter one set of data results while iterating through another.我不确定如何执行此操作,但我想在迭代另一组数据结果时过滤一组数据结果。 What is the correct way to do this?这样做的正确方法是什么? I am trying array_filter but I'm not sure I grasp how to make this work similar to a sql where statement: edit: These below queries are to a MySQL database我正在尝试 array_filter,但我不确定我是否掌握如何使这项工作类似于 sql where 语句:编辑:这些下面的查询是对 MySQL 数据库的

    $arr_years = $wpdb->get_results("SELECT id, submodel_id, year FROM `subyears` ORDER by submodel_id", ARRAY_A);
    $arr_submodels = $wpdb->get_results("SELECT id, model_id, sub_name FROM `submodels` ORDER by model_id", ARRAY_A);
    foreach($arr_submodels as $submodel){
        $filtered = array_filter($arr_years, function ($value,$key){
            return $value == $submodel->id && $key == 'year';
        });
    //do something with $filtered here
    }

Basically I want the equivalent of "where submodel_id == id of the other array".基本上我想要相当于“where submodel_id == id of the other array”。 I think array_map might do what I want but I don't grasp that either, but if that is a better way please let me know and show me how?我认为 array_map 可能会做我想做的,但我也不明白,但如果这是更好的方法,请告诉我并告诉我如何做? Thanks.谢谢。

Edit: added example output snippets of array's: $arr_years:编辑:添加了数组的示例输出片段:$arr_years:

array(50) { [0]=> array(3) { ["id"]=> string(1) "1" ["submodel_id"]=> string(1) "2" ["year"]=> string(4) "2020" } [1]=> array(3) { ["id"]=> string(1) "2" ["submodel_id"]=> string(1) "3" ["year"]=> string(4) "2020" } [2]=> array(3) { ["id"]=> string(1) "3" ["submodel_id"]=> string(1) "4" ["year"]=> string(4) "2020" } [3]=> array(3) { ["id"]=> string(1) "4" ["submodel_id"]=> string(1) "5" ["year"]=> string(4) "2020" } [4]=> array(3) { ["id"]=> string(1) "5" ["submodel_id"]=> string(1) "6" ["year"]=> string(4) "2020" } }

$arr_submodels: $arr_submodels:

array(50) { [0]=> array(3) { ["id"]=> string(1) "2" ["model_id"]=> string(2) "22" ["sub_name"]=> string(13) "RMK KHAOS 155" } [1]=> array(3) { ["id"]=> string(1) "3" ["model_id"]=> string(2) "22" ["sub_name"]=> string(7) "RMK 144" } [2]=> array(3) { ["id"]=> string(1) "4" ["model_id"]=> string(2) "22" ["sub_name"]=> string(11) "PRO-RMK 155" } [3]=> array(3) { ["id"]=> string(1) "5" ["model_id"]=> string(2) "22" ["sub_name"]=> string(11) "PRO-RMK 163" } [4]=> array(3) { ["id"]=> string(1) "6" ["model_id"]=> string(2) "22" ["sub_name"]=> string(11) "PRO-RMK 174" } [5]=> array(3) { ["id"]=> string(1) "7" ["model_id"]=> string(2) "23" ["sub_name"]=> string(7) "SKS 146" } }

The reason I don't do this in the sql is I need to create a comma delimited list of years from the years table for each submodel id...it's a many to one relationship.我不在 sql 中执行此操作的原因是我需要从年份表中为每个子模型 ID 创建一个以逗号分隔的年份列表……这是多对一的关系。

Thanks谢谢

Based on new information, this is what you likely want:根据新信息,这就是您可能想要的:

$arr = $wpdb->get_results("
  SELECT GROUP_CONCAT(sy.year) as years,
  sm.model_id as model_id,
  sm.sub_name as sub_name
  FROM `subyears` sy
  JOIN `submodels` sm
  ON sy.submodel_id=sm.id
  GROUP BY sm.model_id
  ORDER BY sm.model_id
");

The "years" column in the returned array will contain all of the years.返回数组中的“年份”列将包含所有年份。 By default, they are comma separated.默认情况下,它们以逗号分隔。

---OLD ANSWER BELOW--- ---下面的旧答案---

You should do this in the query itself:您应该在查询本身中执行此操作:

$arr = $wpdb->get_results("
  SELECT sy.id, sy.year, sm.id, sm.model_id, sm.sub_name
  FROM `subyears` sy
  JOIN `submodels` sm
  ON sy.submodel_id=sm.id
  ORDER BY sm.model_id
");

If you really insist on doing it outside the query, you need to create an entirely new array:如果您真的坚持在查询之外执行此操作,则需要创建一个全新的数组:

$arr = array();
foreach($arr_years as $year)
  foreach($arr_submodels as $submodel)
    if($year->submodel_id == $submodel->id)
      $arr[] = (object) array_merge((array) $year, (array) $submodel));

Now, $arr contains all the objects from the two arrays merged together where submodel_id == id.现在,$arr 包含合并在一起的两个数组中的所有对象,其中 submodel_id == id。 You can loop through $arr and do what you want.你可以遍历 $arr 并做你想做的。

I used array_merge because I don't know of any function to merge objects.我使用 array_merge 是因为我不知道任何合并对象的函数。 If there is one, it will be more efficient.如果有的话,效率会更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM