0

I have 2 array and this array structur look like.

array 1:

array:21 [▼
  8 => 5.2611505021202
  21 => 1.9708761602636
  4 => 1.9691954688805
] 

array 2:

array:21 [▼
  8 => array:2 [▼
    "id" => 10
    "details" => "Intel Core i5 4GB 1TB"
  ]
  21 => array:2 [▼
    "id" => 14
    "details" => "Intel Core i5 8GB 2TB SSD Touch Bar"
  ]
  4 => array:2 [▼
    "id" => 4
    "details" => "Intel Core i7-8700 GTX1060 8GB 1TB SSD Win10"
  ]
]

the problem is, how can u combine that array and make new array like this :

array:21 [▼
  8 => array:2 [▼
    "id" => 10
    "details" => "Intel Core i5 4GB 1TB"
    "score" => 5.2611505021202 //value from array 1 and input the value to same index [8] -> [8]
  ]
  21 => array:2 [▼
    "id" => 14
    "details" => "Intel Core i5 8GB 2TB SSD Touch Bar"
    "score" => 1.9708761602636 //value from array 1 and input the value to same index [21] -> [21]
  ]
  4 => array:2 [▼
    "id" => 4
    "details" => "Intel Core i7-8700 GTX1060 8GB 1TB SSD Win10"
    "score" => 1.9691954688805 //value from array 1 and input the value to same index [4] -> [4]
  ]
]

is that possible ?, im newb very grateful if someone helps. sorry for my broken english.

1
  • I think you have to write a custom function to create a new array, something not specific to laravel Commented Jun 27, 2020 at 8:36

1 Answer 1

1

I don't think there is something inbuilt in laravel-5.6, but there is something in laravel-6x that would work in your case

but for laravel 5.x you have to write something by your own like below

foreach ($array2 as $key => $item2) {
    if (array_key_exists($key, $array1)) {
        $item2[$key]["score"] = $array1[$key];
    }
}

dd($array2);
Sign up to request clarification or add additional context in comments.

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.