PHP 使用具有多个键/值对的另一个数组来搜索一个数组

问题描述 投票:0回答:1

我正在努力使用另一个数组中的参数来搜索多维数组。

这是两个示例数组和我想要的输出:

Array
(
    [0] => Array
        (
            [product_id] => 5
            [product_name] => Miel de lavande bio
            [feats] => Array
                (
                    [0] => Array
                        (
                            [name] => Intensité
                            [value] => 3
                            [id_feature] => 1
                            [position] => 0
                            [id_feature_value] => 3
                        )

                    [1] => Array
                        (
                            [name] => Texture
                            [value] => Liquide
                            [id_feature] => 2
                            [position] => 1
                            [id_feature_value] => 4
                        )

                    [2] => Array
                        (
                            [name] => Cristalisation
                            [value] => Lente
                            [id_feature] => 3
                            [position] => 2
                            [id_feature_value] => 7
                        )

                    [3] => Array
                        (
                            [name] => Saveur
                            [value] => Fruitée
                            [id_feature] => 4
                            [position] => 3
                            [id_feature_value] => 10
                        )

                    [4] => Array
                        (
                            [name] => Saveur
                            [value] => Florale
                            [id_feature] => 4
                            [position] => 3
                            [id_feature_value] => 11
                        )

                    [5] => Array
                        (
                            [name] => Notes
                            [value] => amandes
                            [id_feature] => 5
                            [position] => 4
                            [id_feature_value] => 34
                        )

                    [6] => Array
                        (
                            [name] => Notes
                            [value] => fruits exotiques
                            [id_feature] => 5
                            [position] => 4
                            [id_feature_value] => 35
                        )

                    [7] => Array
                        (
                            [name] => Région
                            [value] => Bretagne
                            [id_feature] => 6
                            [position] => 5
                            [id_feature_value] => 17
                        )

                    [8] => Array
                        (
                            [name] => Utilisation
                            [value] => sucrer
                            [id_feature] => 7
                            [position] => 6
                            [id_feature_value] => 31
                        )

                    [9] => Array
                        (
                            [name] => Utilisation
                            [value] => desserts
                            [id_feature] => 7
                            [position] => 6
                            [id_feature_value] => 33
                        )

                )

        )

    [1] => Array
        (
            [product_id] => 6
            [product_name] => Miel de sarrasin
            [feats] => Array
                (
                    [0] => Array
                        (
                            [name] => Intensité
                            [value] => 5
                            [id_feature] => 1
                            [position] => 0
                            [id_feature_value] => 29
                        )

                    [1] => Array
                        (
                            [name] => Saveur
                            [value] => Boisée
                            [id_feature] => 4
                            [position] => 3
                            [id_feature_value] => 12
                        )

                    [2] => Array
                        (
                            [name] => Saveur
                            [value] => Gourmande
                            [id_feature] => 4
                            [position] => 3
                            [id_feature_value] => 13
                        )

                    [3] => Array
                        (
                            [name] => Notes
                            [value] => réglisse
                            [id_feature] => 5
                            [position] => 4
                            [id_feature_value] => 38
                        )

                    [4] => Array
                        (
                            [name] => Notes
                            [value] => torréfié
                            [id_feature] => 5
                            [position] => 4
                            [id_feature_value] => 39
                        )

                    [5] => Array
                        (
                            [name] => Utilisation
                            [value] => gourmandise
                            [id_feature] => 7
                            [position] => 6
                            [id_feature_value] => 30
                        )

                    [6] => Array
                        (
                            [name] => Utilisation
                            [value] => desserts
                            [id_feature] => 7
                            [position] => 6
                            [id_feature_value] => 33
                        )

                )

        )

    [2] => Array
        (
            [product_id] => 7
            [product_name] => Miel de tilleul
            [feats] => Array
                (
                    [0] => Array
                        (
                            [name] => Intensité
                            [value] => 3
                            [id_feature] => 1
                            [position] => 0
                            [id_feature_value] => 3
                        )

                    [1] => Array
                        (
                            [name] => Texture
                            [value] => Crémeuse
                            [id_feature] => 2
                            [position] => 1
                            [id_feature_value] => 5
                        )

                    [2] => Array
                        (
                            [name] => Saveur
                            [value] => Boisée
                            [id_feature] => 4
                            [position] => 3
                            [id_feature_value] => 12
                        )

                    [3] => Array
                        (
                            [name] => Saveur
                            [value] => Fraîche
                            [id_feature] => 4
                            [position] => 3
                            [id_feature_value] => 14
                        )

                    [4] => Array
                        (
                            [name] => Notes
                            [value] => mentholée
                            [id_feature] => 5
                            [position] => 4
                            [id_feature_value] => 36
                        )

                    [5] => Array
                        (
                            [name] => Notes
                            [value] => sous-bois
                            [id_feature] => 5
                            [position] => 4
                            [id_feature_value] => 37
                        )

                    [6] => Array
                        (
                            [name] => Utilisation
                            [value] => sucrer
                            [id_feature] => 7
                            [position] => 6
                            [id_feature_value] => 31
                        )

                    [7] => Array
                        (
                            [name] => Utilisation
                            [value] => desserts
                            [id_feature] => 7
                            [position] => 6
                            [id_feature_value] => 33
                        )

                )

        )

)

第二个数组:

Array
(
    [0] => Array
        (
            [name] => Intensité
            [value] => 2
        )

    [1] => Array
        (
            [name] => Saveurs
            [value] => Boisée
        )

    [2] => Array
        (
            [name] => Saveurs
            [value] => Gourmande
        )

    [3] => Array
        (
            [name] => Utilisation
            [value] => gourmandise
        )

    [4] => Array
        (
            [name] => Utilisation
            [value] => sucrer
        )

)

我想使用第二个数组“搜索”并从第一个数组中提取数据。 在这个例子中,我期望使用这两个数组得到的输出是:

Array
(
    [0] => Array
        (
            [product_id] => 6
            [product_name] => Miel de sarrasin
            [feats] => Array
                (
                    [0] => Array
                        (
                            [name] => Intensité
                            [value] => 5
                            [id_feature] => 1
                            [position] => 0
                            [id_feature_value] => 29
                        )

                    [1] => Array
                        (
                            [name] => Saveur
                            [value] => Boisée
                            [id_feature] => 4
                            [position] => 3
                            [id_feature_value] => 12
                        )

                    [2] => Array
                        (
                            [name] => Saveur
                            [value] => Gourmande
                            [id_feature] => 4
                            [position] => 3
                            [id_feature_value] => 13
                        )

                    [3] => Array
                        (
                            [name] => Notes
                            [value] => réglisse
                            [id_feature] => 5
                            [position] => 4
                            [id_feature_value] => 38
                        )

                    [4] => Array
                        (
                            [name] => Notes
                            [value] => torréfié
                            [id_feature] => 5
                            [position] => 4
                            [id_feature_value] => 39
                        )

                    [5] => Array
                        (
                            [name] => Utilisation
                            [value] => gourmandise
                            [id_feature] => 7
                            [position] => 6
                            [id_feature_value] => 30
                        )

                    [6] => Array
                        (
                            [name] => Utilisation
                            [value] => desserts
                            [id_feature] => 7
                            [position] => 6
                            [id_feature_value] => 33
                        )

                )

        )

)

有什么想法或建议吗?

我玩过循环和 array_intersect_key 但到目前为止无法找到解决方案。

php arrays multidimensional-array
1个回答
0
投票

假设您的第一个数组(所有产品中)名为

$all
,第二个数组(搜索数组)名为
$search
,如果所需的逻辑是匹配具有搜索数组中任何功能的任何产品,那么这可能对你有用:

$result = array_filter($all, function ($product) use ($search) {
    return (bool) array_filter($product['feats'], function ($feat) use ($search) {
        return (bool) array_filter($search, function ($searchFeat) use ($feat) {
            return ($feat['name'] == $searchFeat['name'] && $feat['value'] == $searchFeat['value']);
        });
    });
});

print_r($result);
© www.soinside.com 2019 - 2024. All rights reserved.