根据另一个数组的内容过滤多维数组的内容

时间:2018-10-01 11:54:03

标签: php arrays

我需要删除我的产品数组中类别不在控件数组内容中的所有条目。我确实在SO上看到了类似的问题,但是我没有找到可行的解决方案,而且很多问题都与简单数组而不是多维数组有关。

我的$ products数组的示例是

Array
(
[0] => Array
    (
        [id] => 3240
        [category] => 274
        [name] => Banham S464 Euro Double Cylinder 72mm 36/36 (31/10/31) KD PB
        [model] => L13304
        [image] => aldridge/L13304.jpg
        [price] => 164.3400
        [quantity] => 1
        [weight] => 314
        [final_price] => 164.34
        [onetime_charges] => 0
        [tax_class_id] => 5
        [attributes] => 
        [attributes_values] => 
        [products_priced_by_attribute] => 0
        [product_is_free] => 0
        [products_discount_type] => 0
        [products_discount_type_from] => 0
        [products_virtual] => 0
        [product_is_always_free_shipping] => 0
    )

[1] => Array
    (
        [id] => 4395
        [category] => 163
        [name] => EVVA EPS L111 & S363 Cylinders To Suit Banham Locks NP KA Pair - LH
        [model] => L22135
        [image] => aldridge/L22135.jpg
        [price] => 173.7630
        [quantity] => 1
        [weight] => 360
        [final_price] => 173.763
        [onetime_charges] => 0
        [tax_class_id] => 5
        [attributes] => 
        [attributes_values] => 
        [products_priced_by_attribute] => 0
        [product_is_free] => 0
        [products_discount_type] => 0
        [products_discount_type_from] => 0
        [products_virtual] => 0
        [product_is_always_free_shipping] => 0
    )

[2] => Array
    (
        [id] => 370:b97dbdf0f1fbaab5ca861ac73bfe530d
        [category] => 144
        [name] => L&F 5880 Furniture Rim Lock - Black Finish
        [model] => 5880-BL
        [image] => 5880BL.jpg
        [price] => 4.7000
        [quantity] => 1
        [weight] => 120
        [final_price] => 4.7
        [onetime_charges] => 0
        [tax_class_id] => 5
        [attributes] => Array
            (
                [45] => 243
            )

        [attributes_values] => 
        [products_priced_by_attribute] => 0
        [product_is_free] => 0
        [products_discount_type] => 0
        [products_discount_type_from] => 0
        [products_virtual] => 0
        [product_is_always_free_shipping] => 0
    )

)

我的$ control数组是

Array
(
[0] => Array
    (
        [categories_id] => 163
    )


[1] => Array
    (
        [categories_id] => 179
    )

[2] => Array
    (
        [categories_id] => 274
    )

) 

产品数组中的最后一项的类别值为144,不在控件数组中,因此应将其删除。

根据我尝试过的其他帖子中的信息

$products = array_flip( $products );
$products = array_diff( $products, $control );
$products = array_flip( $products );

但是当我执行print_r($ products)时,我刚得到Array()

var_export($ products)

array ( 0 => array ( 'id' => 3240, 'category' => '274', 'name' => 'Banham S464 Euro Double Cylinder 72mm 36/36 (31/10/31) KD PB', 'model' => 'L13304', 'image' => 'aldridge/L13304.jpg', 'price' => '164.3400', 'quantity' => 1, 'weight' => 314, 'final_price' => 164.340000000000003410605131648480892181396484375, 'onetime_charges' => 0, 'tax_class_id' => '5', 'attributes' => '', 'attributes_values' => '', 'products_priced_by_attribute' => '0', 'product_is_free' => '0', 'products_discount_type' => '0', 'products_discount_type_from' => '0', 'products_virtual' => '0', 'product_is_always_free_shipping' => '0', ), 1 => array ( 'id' => 4395, 'category' => '274', 'name' => 'EVVA EPS L111 & S363 Cylinders To Suit Banham Locks NP KA Pair - LH', 'model' => 'L22135', 'image' => 'aldridge/L22135.jpg', 'price' => '173.7630', 'quantity' => 1, 'weight' => 360, 'final_price' => 173.763000000000005229594535194337368011474609375, 'onetime_charges' => 0, 'tax_class_id' => '5', 'attributes' => '', 'attributes_values' => '', 'products_priced_by_attribute' => '0', 'product_is_free' => '0', 'products_discount_type' => '0', 'products_discount_type_from' => '0', 'products_virtual' => '0', 'product_is_always_free_shipping' => '0', ), 2 => array ( 'id' => 4396, 'category' => '274', 'name' => 'EVVA EPS L111 & S363 Cylinders To Suit Banham Locks NP KA Pair - RH', 'model' => 'L22136', 'image' => 'aldridge/L22136.jpg', 'price' => '173.7630', 'quantity' => 1, 'weight' => 360, 'final_price' => 173.763000000000005229594535194337368011474609375, 'onetime_charges' => 0, 'tax_class_id' => '5', 'attributes' => '', 'attributes_values' => '', 'products_priced_by_attribute' => '0', 'product_is_free' => '0', 'products_discount_type' => '0', 'products_discount_type_from' => '0', 'products_virtual' => '0', 'product_is_always_free_shipping' => '0', ), 3 => array ( 'id' => '370:b97dbdf0f1fbaab5ca861ac73bfe530d', 'category' => '144', 'name' => 'L&F 5880 Furniture Rim Lock - Black Finish', 'model' => '5880-BL', 'image' => '5880BL.jpg', 'price' => '4.7000', 'quantity' => 1, 'weight' => 120, 'final_price' => 4.70000000000000017763568394002504646778106689453125, 'onetime_charges' => 0, 'tax_class_id' => '5', 'attributes' => array ( 45 => '243', ), 'attributes_values' => '', 'products_priced_by_attribute' => '0', 'product_is_free' => '0', 'products_discount_type' => '0', 'products_discount_type_from' => '0', 'products_virtual' => '0', 'product_is_always_free_shipping' => '0', ), )

var_export($ control)

array ( 0 => array ( 'categories_id' => '152', ), 1 => array ( 'categories_id' => '153', ), 2 => array ( 'categories_id' => '154', ), 3 => array ( 'categories_id' => '155', ), 4 => array ( 'categories_id' => '156', ), 5 => array ( 'categories_id' => '157', ), 6 => array ( 'categories_id' => '158', ), 7 => array ( 'categories_id' => '159', ), 8 => array ( 'categories_id' => '160', ), 9 => array ( 'categories_id' => '161', ), 10 => array ( 'categories_id' => '162', ), 11 => array ( 'categories_id' => '163', ), 12 => array ( 'categories_id' => '164', ), 13 => array ( 'categories_id' => '165', ), 14 => array ( 'categories_id' => '166', ), 15 => array ( 'categories_id' => '167', ), 16 => array ( 'categories_id' => '168', ), 17 => array ( 'categories_id' => '169', ), 18 => array ( 'categories_id' => '170', ), 19 => array ( 'categories_id' => '171', ), 20 => array ( 'categories_id' => '172', ), 21 => array ( 'categories_id' => '173', ), 22 => array ( 'categories_id' => '174', ), 23 => array ( 'categories_id' => '175', ), 24 => array ( 'categories_id' => '176', ), 25 => array ( 'categories_id' => '177', ), 26 => array ( 'categories_id' => '178', ), 27 => array ( 'categories_id' => '179', ), 28 => array ( 'categories_id' => '180', ), 29 => array ( 'categories_id' => '181', ), 30 => array ( 'categories_id' => '182', ), 31 => array ( 'categories_id' => '183', ), 32 => array ( 'categories_id' => '184', ), 33 => array ( 'categories_id' => '185', ), 34 => array ( 'categories_id' => '186', ), 35 => array ( 'categories_id' => '187', ), 36 => array ( 'categories_id' => '188', ), 37 => array ( 'categories_id' => '189', ), 38 => array ( 'categories_id' => '190', ), 39 => array ( 'categories_id' => '191', ), 40 => array ( 'categories_id' => '192', ), 41 => array ( 'categories_id' => '193', ), 42 => array ( 'categories_id' => '194', ), 43 => array ( 'categories_id' => '195', ), 44 => array ( 'categories_id' => '196', ), 45 => array ( 'categories_id' => '197', ), 46 => array ( 'categories_id' => '198', ), 47 => array ( 'categories_id' => '199', ), 48 => array ( 'categories_id' => '200', ), 49 => array ( 'categories_id' => '201', ), 50 => array ( 'categories_id' => '202', ), 51 => array ( 'categories_id' => '203', ), 52 => array ( 'categories_id' => '204', ), 53 => array ( 'categories_id' => '205', ), 54 => array ( 'categories_id' => '206', ), 55 => array ( 'categories_id' => '207', ), 56 => array ( 'categories_id' => '208', ), 57 => array ( 'categories_id' => '209', ), 58 => array ( 'categories_id' => '210', ), 59 => array ( 'categories_id' => '211', ), 60 => array ( 'categories_id' => '212', ), 61 => array ( 'categories_id' => '213', ), 62 => array ( 'categories_id' => '214', ), 63 => array ( 'categories_id' => '215', ), 64 => array ( 'categories_id' => '216', ), 65 => array ( 'categories_id' => '217', ), 66 => array ( 'categories_id' => '218', ), 67 => array ( 'categories_id' => '219', ), 68 => array ( 'categories_id' => '220', ), 69 => array ( 'categories_id' => '221', ), 70 => array ( 'categories_id' => '222', ), 71 => array ( 'categories_id' => '223', ), 72 => array ( 'categories_id' => '224', ), 73 => array ( 'categories_id' => '225', ), 74 => array ( 'categories_id' => '226', ), 75 => array ( 'categories_id' => '227', ), 76 => array ( 'categories_id' => '228', ), 77 => array ( 'categories_id' => '229', ), 78 => array ( 'categories_id' => '230', ), 79 => array ( 'categories_id' => '231', ), 80 => array ( 'categories_id' => '232', ), 81 => array ( 'categories_id' => '233', ), 82 => array ( 'categories_id' => '234', ), 83 => array ( 'categories_id' => '235', ), 84 => array ( 'categories_id' => '236', ), 85 => array ( 'categories_id' => '237', ), 86 => array ( 'categories_id' => '238', ), 87 => array ( 'categories_id' => '239', ), 88 => array ( 'categories_id' => '240', ), 89 => array ( 'categories_id' => '241', ), 90 => array ( 'categories_id' => '242', ), 91 => array ( 'categories_id' => '243', ), 92 => array ( 'categories_id' => '244', ), 93 => array ( 'categories_id' => '245', ), 94 => array ( 'categories_id' => '246', ), 95 => array ( 'categories_id' => '247', ), 96 => array ( 'categories_id' => '248', ), 97 => array ( 'categories_id' => '249', ), 98 => array ( 'categories_id' => '250', ), 99 => array ( 'categories_id' => '251', ), 100 => array ( 'categories_id' => '252', ), 101 => array ( 'categories_id' => '253', ), 102 => array ( 'categories_id' => '254', ), 103 => array ( 'categories_id' => '255', ), 104 => array ( 'categories_id' => '256', ), 105 => array ( 'categories_id' => '257', ), 106 => array ( 'categories_id' => '258', ), 107 => array ( 'categories_id' => '259', ), 108 => array ( 'categories_id' => '260', ), 109 => array ( 'categories_id' => '261', ), 110 => array ( 'categories_id' => '262', ), 111 => array ( 'categories_id' => '263', ), 112 => array ( 'categories_id' => '264', ), 113 => array ( 'categories_id' => '265', ), 114 => array ( 'categories_id' => '266', ), 115 => array ( 'categories_id' => '267', ), 116 => array ( 'categories_id' => '268', ), 117 => array ( 'categories_id' => '269', ), 118 => array ( 'categories_id' => '270', ), 119 => array ( 'categories_id' => '271', ), 120 => array ( 'categories_id' => '272', ), 121 => array ( 'categories_id' => '273', ), 122 => array ( 'categories_id' => '274', ), 123 => array ( 'categories_id' => '275', ), 124 => array ( 'categories_id' => '276', ), 125 => array ( 'categories_id' => '277', ), 126 => array ( 'categories_id' => '278', ), 127 => array ( 'categories_id' => '279', ), 128 => array ( 'categories_id' => '280', ), 129 => array ( 'categories_id' => '281', ), 130 => array ( 'categories_id' => '282', ), 131 => array ( 'categories_id' => '283', ), 132 => array ( 'categories_id' => '284', ), 133 => array ( 'categories_id' => '285', ), 134 => array ( 'categories_id' => '286', ), 135 => array ( 'categories_id' => '287', ), 136 => array ( 'categories_id' => '288', ), 137 => array ( 'categories_id' => '289', ), )

2 个答案:

答案 0 :(得分:0)

我建议:

// Build a single array of categories IDs, that are allowed
$categories = [];
foreach ($control as $value) {
    $categories[] = $value['categories_id'];
}

// Filter values that are not in the $categories
$result = array_filter($products, function($val) {
    return in_array($val['category'], $categories);
});

print_r($result);

答案 1 :(得分:0)

您应该可以通过简单的array_intersect来做到这一点。

//flatten control array
$control = array_column($control, "categories_id");

// make $products associative.
$products = array_column($products, Null, "category");

//filter with array_intersect_key
$filtered = array_intersect_key($products, array_flip($control));

(可选)您可以添加array_values以删除最后一行上的关联键,例如:

//filter with array_intersect_key
$filtered = array_values(array_intersect_key($products, array_flip($control)));

如果您也想要一种衬板。

$filtered = array_values(array_intersect_key(array_column($products, Null, "category"), array_flip(array_column($control, "categories_id"))));

但这在我看来很难读懂。