-1

There are two arrays. The first:

Array
(
    [0] => Array
        (
            [0] => Apple
            [1] => big
        )

    [1] => Array
        (
            [0] => Orange
            [1] => medium
        )

    [2] => Array
        (
            [0] => Orange
            [1] => big
        )
)

second

Array
(
[entries] => Array
        (
            [0] => Array
                (
                    [name_item] => Apple
                    [price_item] => 70
                    [image_item] => img/apple45645.jpg
                    [_id] => 608e9eb364323555fb0002b2
                )
            [1] => Array
                (
                    [name_item] => Orange
                    [price_item] => 90
                    [image_item] => img/ORANGE777.jpg
                    [_id] => 6092bc293661377e0700007c
                )
        )
)

I need to add data from the second array to the first one. I'm interested in the price_item and image_item fields, which correspond to the name of the array. The problem is that I don't know how to retrieve the content, relative to the object I'm looking for. Okay, if I had a key, I would use it to search and get the content, but here, in this situation, I have stupor. Is it even possible?

I wanted to get the final array like this:

Array
(
    [0] => Array
        (
            [0] => Apple
            [1] => big
            [price_item] => 70
            [image_item] => img/apple45645.jpg
        )

    [1] => Array
        (
            [0] => Orange
            [1] => medium
            [price_item] => 90
            [image_item] => img/ORANGE777.jpg
        )

    [2] => Array
        (
            [0] => Orange
            [1] => big
            [price_item] => 90
            [image_item] => img/ORANGE777.jpg
        )
)

1 Answers1

0

Create a for to walk through your first array and the inside this for loop you need to create another loop to walk through the second array and compare the values, and if matchs you will add the two keys inside the first array.

<?php
$arrayOne = [
  [
    "Apple",
    "big",
  ],

  [
    "Orange",
    "medium",
  ],

  [
    "Orange",
    "big",
  ],
];
$arrayTwo = [
  "entries" =>
  [
    [
      "name_item" => "Apple",
      "price_item" => "70",
      "image_item" => "img/apple45645.jpg",
      "_id" => "608e9eb364323555fb0002b2",
    ],
    [
      "name_item" => "Orange",
      "price_item" => "90",
      "image_item" => "img/ORANGE777.jpg",
      "_id" => "6092bc293661377e0700007c",
    ],
  ],
];

for ($i = 0; $i < count($arrayOne); $i++) {
  foreach ($arrayTwo['entries'] as $key => $val) {
    if ($val['name_item'] === $arrayOne[$i][0]) {
      $arrayOne[$i]['price_item'] = $val['price_item'];
      $arrayOne[$i]['image_item'] = $val['image_item'];
      break;
    }
  }
}
var_dump($arrayOne);

Result:

var_dump formatted with xdebug

array (size=3)
  0 => 
    array (size=4)
      0 => string 'Apple' (length=5)
      1 => string 'big' (length=3)
      'price_item' => string '70' (length=2)
      'image_item' => string 'img/apple45645.jpg' (length=18)
  1 => 
    array (size=4)
      0 => string 'Orange' (length=6)
      1 => string 'medium' (length=6)
      'price_item' => string '90' (length=2)
      'image_item' => string 'img/ORANGE777.jpg' (length=17)
  2 => 
    array (size=4)
      0 => string 'Orange' (length=6)
      1 => string 'big' (length=3)
      'price_item' => string '90' (length=2)
      'image_item' => string 'img/ORANGE777.jpg' (length=17)
André Walker
  • 588
  • 10
  • 30