1

After decoding a json string that represents a multidimensional object array, I am trying to map one of its objects to another of its objects based on matching id values.

My input:

$data = array (
  0 => 
  (object) array(
     'products' => 
    array (
      0 => 
      (object) array(
         'Part ID' => '001',
         'name' => 'Product Name',
         'category' => 'Product Category',
         'description' => 'Product Description',
         'id' => '111xyz',
      ),
      1 => 
      (object) array(
         'Part ID' => '002',
         'name' => 'Product Name 2',
         'category' => 'Product Category 2',
         'description' => 'Product Description 2',
         'id' => '333xyz',
      ),
      2 => 
      (object) array(
         'Part ID' => '003',
         'name' => 'Product Name 3',
         'category' => 'Product Category 3',
         'description' => 'Product Description 3',
         'id' => '444xyz',
      ),
      3 => 
      (object) array(
         'Part ID' => '004',
         'name' => 'Product Name 4',
         'category' => 'Product Category',
         'description' => 'Product Description',
         'id' => '666xyz',
         'features_img_id' => 
        array (
          0 => 'f1',
          1 => 'f2',
          2 => 'f3',
        ),
      ),
    ),
  ),
  1 => 
  (object) array(
     'assets' => 
    array (
      0 => 
      (object) array(
         'File Name' => 'Some file name',
         'url' => 'www.123.com/x.jpg',
         'id' => '111xyz',
      ),
      1 => 
      (object) array(
         'File Name' => 'Feature 1 file',
         'url' => 'www.123.com/f1.jpg',
         'id' => 'f1',
      ),
      2 => 
      (object) array(
         'File Name' => 'Feature 2 file',
         'url' => 'www.123.com/f2.jpg',
         'id' => 'f2',
      ),
      3 => 
      (object) array(
         'File Name' => 'Feature 2 file',
         'url' => 'www.123.com/f3.jpg',
         'id' => 'f3',
      ),
    ),
  ),
);

Desired result:

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
                )
            )
    )

Below is what I tried, but it is only producing the one array.

$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);

I don't know how to target the 'feature_img_id' array and match it with the assets ids and add it to the $processedProducts.

2
  • Ok. I saw that you added some new info about your problem as an answer. Stack Overflow will consider it as not an answer and flag it, so I recommend you to delete it and add that new scenario below a Edit: section at the bottom of your original question. Now, the features_img: you want it to be an array of s_id (["f1","f2","f3"]) or and array of url (["www.123.com/f1.jpg","www.123.com/f2.jpg","www.123.com/f3.jpg"])? Commented Aug 23, 2019 at 18:32
  • By the way, I edited my answer to cover this new scenario. Hope it helps Commented Aug 23, 2019 at 19:12

2 Answers 2

1

You can do it this way:

$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);
}

Will lead to the resulting array below:

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
                )

        )

)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you @Caconde. I'll try it out.
Thank you for helping me out. I'm still stuck to sort this out. You assigned the products object as $products = $data[0]->products; and it assign the array of objects to $products variable. How the same thing can be achieved with foreach loop without assigning it to new array, I mean assing $products = $data[0]->products; to variable insteat of array? thank
I don't know if I understand your question here. The code in the answer isn't working?
Sorry @Caconde for my poor explanation. I edited the question and the desired result. Will appreciate any feedback. thanks
Ok, @Chip. changed the answer to fit your scenario.
0
  1. Decode your json as an object array.
  2. Create an associative map to find id-related values from the assets object.
  3. Loop the products data and push values which are found in the map.
  4. Loop over the inconsistently declared features_img_id data and push values which are found in the map.
  5. Unset the object properties that are unwanted.

Code: (Demo)

$map = array_column($array[1]->assets, 'url', 'id');

foreach ($array[0]->products as $obj) {
    if (isset($map[$obj->id])) {
        $obj->url = $map[$obj->id];
    }
    foreach ($obj->features_img_id ?? [] as $imgId) {
        if (isset($map[$imgId])) {
            $obj->feature_img_url[] = $map[$imgId];
        }
    }
    unset($obj->id, $obj->features_img_id);
}
var_export($array[0]->products);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.