根据匹配值从两个数组创建新数组

时间:2019-08-22 17:46:08

标签: php

尝试根据匹配值从两个对象数组创建一个新数组,但不知道如何实现。

        $dData = '[
            {
                "products": [
                    {
                        "Part ID" : "001",
                        "name" : "Product Name",
                        "category" : "Product Category",
                        "description" : "Product Description",
                        "id" : "111xyz",
                    },
                    {
                        "Part ID" : "002",
                        "name" : "Product Name 2",
                        "category" : "Product Category 2",
                        "description" : "Product Description 2",
                        "id" : "333xyz"
                    },
                    {
                        "Part ID" : "003",
                        "name" : "Product Name 3",
                        "category" : "Product Category 3",
                        "description" : "Product Description 3",
                        "id" : "444xyz"
                    },  
                    {
                        "Part ID" : "004",
                        "name" : "Product Name 4",
                        "category" : "Product Category",
                        "description" : "Product Description",
                        "id" : "666xyz",
                        "features_img_id":["f1","f2","f3"]
                    }]
                },
                {
                "assets": [
                    {
                        "File Name" : "Some file name",
                        "url" : "www.123.com/x.jpg",
                        "id" : "111xyz"
                    },
                    {
                         "File Name" : "Feature 1 file",
                        "url" : "www.123.com/f1.jpg",
                        "id" : "f1"
                    },
                    {
                         "File Name" : "Feature 2 file",
                        "url" : "www.123.com/f2.jpg",
                        "id" : "f2"
                    },
                    {
                         "File Name" : "Feature 2 file",
                        "url" : "www.123.com/f3.jpg",
                        "id" : "f3"
                    }]
            }
]';

所需的新数组结果

Array
    (
        [0] => stdClass Object
            (
                [Part ID] => 001
                [name] => Product Name 1
                [category] => Product Category
                [description] => Product Description
                [url] => www.123.com/x.jpg
            )

        [1] => stdClass Object
            (
                [Part ID] => 002
                [name] => Product Name 2
                [category] => Product Category 2
                [description] => Product Description 2
            )

        [2] => stdClass Object
            (
                [Part ID] => 003
                [name] => Product Name 3
                [category] => Product Category 3
                [description] => Product Description 3
            )

        [3] => stdClass Object
            (
                [Part ID] => 004
                [name] => Product Name 4
                [category] => Product Category 4
                [description] => Product Description 4
                [feature_img-url] => Array
                (
                    [0] => www.123.com/f1.jpg
                    [1] => www.123.com/f3.jpg
                    [2] => www.123.com/f3.jpg
                )
            )
    )

下面是我尝试过的方法,但它只产生一个数组

$data = json_decode($dData, false);
$products = $data[0]->products;
$assets = $data[1]->assets;

$newArr;
foreach ($products as $key => $value) {
    $newArr = $value->features_img;
}

$processedProducts = $products;
foreach ($processedProducts as $key => $product) {
    foreach ($assets as $asset) {
        if ($asset->id == $product->id) {
            $product->url = $asset->url;
        } elseif ($asset->id == in_array($asset->id, $newArr)) {
            $product->features_image_url = $asset->url;
        } else {
            //code
        }
    }
    unset($product->id);    
}    
print_r($processedProducts);

如果我完全是新手,我要道歉。不知道如何定位'feature_img_id'数组并将其与资产ID匹配并将其添加到$ processedProducts中。请查看所需的结果以获取想法。

1 个答案:

答案 0 :(得分:1)

您可以这样做:

$dData = '[
            {
                "products": [
                    {
                        "Part ID" : "001",
                        "name" : "Product Name",
                        "category" : "Product Category",
                        "description" : "Product Description",
                        "id" : "111xyz"
                    },
                    {
                        "Part ID" : "002",
                        "name" : "Product Name 2",
                        "category" : "Product Category 2",
                        "description" : "Product Description 2",
                        "id" : "333xyz"
                    },
                    {
                        "Part ID" : "003",
                        "name" : "Product Name 3",
                        "category" : "Product Category 3",
                        "description" : "Product Description 3",
                        "id" : "444xyz"
                    },  
                    {
                        "Part ID" : "004",
                        "name" : "Product Name 4",
                        "category" : "Product Category",
                        "description" : "Product Description",
                        "id" : "666xyz",
                        "features_img_id":["f1","f2","f3"]
                    }]
                },
                {
                "assets": [
                    {
                        "File Name" : "Some file name",
                        "url" : "www.123.com/x.jpg",
                        "id" : "111xyz"
                    },
                    {
                         "File Name" : "Feature 1 file",
                        "url" : "www.123.com/f1.jpg",
                        "id" : "f1"
                    },
                    {
                         "File Name" : "Feature 2 file",
                        "url" : "www.123.com/f2.jpg",
                        "id" : "f2"
                    },
                    {
                         "File Name" : "Feature 2 file",
                        "url" : "www.123.com/f3.jpg",
                        "id" : "f3"
                    }]
            }
]';

$data = json_decode($dData, false);
$products = $data[0]->products;
$assets = $data[1]->assets;

$processedProducts = $products;
foreach($processedProducts as $key => $product) {
    foreach($assets as $asset) {
        if ($asset->id == $product->id) {
            $product->url = $asset->url;    
        }
        if (isset($product->features_img_id) && in_array($asset->id, $product->features_img_id)) {
            $product->feature_image_url[] = $asset->url;    
        }
    }
    unset($product->id);
    unset($product->features_img_id);
}

将导致下面的结果数组:

Array
(
    [0] => stdClass Object
        (
            [Part ID] => 001
            [name] => Product Name
            [category] => Product Category
            [description] => Product Description
            [url] => www.123.com/x.jpg
        )

    [1] => stdClass Object
        (
            [Part ID] => 002
            [name] => Product Name 2
            [category] => Product Category 2
            [description] => Product Description 2
        )

    [2] => stdClass Object
        (
            [Part ID] => 003
            [name] => Product Name 3
            [category] => Product Category 3
            [description] => Product Description 3
        )

    [3] => stdClass Object
        (
            [Part ID] => 004
            [name] => Product Name 4
            [category] => Product Category
            [description] => Product Description
            [feature_image_url] => Array
                (
                    [0] => www.123.com/f1.jpg
                    [1] => www.123.com/f2.jpg
                    [2] => www.123.com/f3.jpg
                )

        )

)
相关问题