Questions tagged [array-merge]

Array-merge means elements of one or more arrays will be merged together into a single array.

Array-merge means merging the elements of one or more arrays together into a single array so that the values of one are appended to the end of the previous one.

array array_merge ( array $array1 [, array $... ] )

Example

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

Result

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

Reference

819 questions
4
votes
5 answers

merge duplicate objects in an array and combine sub array of each object

I am trying to merge objects based off of Id, and merge each array that lives inside each account (object), but instead of merging the contents of accountList, the code overwrites the array, if there is a matching id. I've made a new array and used…
Ali Khalil
  • 126
  • 1
  • 11
4
votes
4 answers

Laravel array_merge(): Argument #2 is not an array In ServiceProvider.php

I can not do php artisan serve anymore, it says : In ServiceProvider.php line 59: array_merge(): Argument #2 is not an array Line 59 code is in ServiceProvider.php: $this->app['config']->set($key, array_merge(require $path, $config)); I…
slekniws
  • 139
  • 1
  • 2
  • 6
4
votes
3 answers

Python algorithm to compare two sorted lists and count how many elements are the same

I have to design an algorithm that compares two sorted lists of the same length and return the number of common values between them. So if I have two lists a = [2, 9, 15, 27, 36, 40] and b = [9, 11, 15, 23, 36, 44], the algorithm should return the…
MacGenius
  • 199
  • 4
  • 16
4
votes
2 answers

Array Merge PHP keeps creating Child/Dimensional Array

I have been trying to solve this issue for the few days. I have gotten nowhere with it. My website has an option to choose what subjects you are doing in school: The front-end part works great and I am able to get save the result in my table in the…
D. 777KLM
  • 450
  • 1
  • 9
  • 27
4
votes
8 answers

Merge second array into first array where row value in first array matches row key in second array

I would like to merge the associative elements from my second array into my first array where the second array's subarray key matches a row's epg_channel_id value. First array: [ [ 'num' => 1, 'name' => 'name 1', …
Alvaro Louzada
  • 433
  • 1
  • 6
  • 23
4
votes
2 answers

Is array_merge perform reindexing?

Suppose I have an associative array with keys that are alphabetic string, and if I merge something to this array, It will merge successfully without reindexing like $arr1 = array('john'=>'JOHN', 'marry'=>'Marry'); $arr1 =…
SNishant
  • 103
  • 11
4
votes
1 answer

Merge rows between two arrays of objects based on column value

After I merged two arrays like this array_merge($array1, $array2);, it becomes like this: array(10) { [0]=> object(stdClass) (2) { ["id"]=> string(1) "1" ["text"]=> string(5) "one" } [1]=> object(stdClass) (2) { …
112233
  • 2,406
  • 3
  • 38
  • 88
4
votes
4 answers

Ruby: Merging nested array between each other depending on a condition

What would be the best way to merge arrays nested in an array that shares at least an element ? Here's an example: some_method([[1, 2], [2, 3], [4, 5]]) #=> [[1, 2, 3], [4, 5]] some_method([[1, 2], [2, 3], [3, 4], [5,6]]) #=> [[1, 2, 3, 4], [5, 6]]
David B.
  • 788
  • 1
  • 11
  • 21
4
votes
4 answers

Merge multi-dimensional arrays and sum column values which share a common value in another column

I have 3 arrays for storing posts,comments, and likes. These are the JSON strings: //comments JSON (stores user and comment points) $comments='[ { "user": "5", "points": "12" }, { "user": "2", "points":…
version 2
  • 1,049
  • 3
  • 15
  • 36
4
votes
2 answers

PHP merge array if empty

Is there a nice way to merge two arrays in PHP. My $defaults-array contains default values. If the $properties-array contains an empty string I want to use the value from the $defaults-array. My code so far looks as following: $defaults =…
pbaldauf
  • 1,671
  • 2
  • 23
  • 32
4
votes
7 answers

How to simulate the SQL LEFT JOIN operation using PHP arrays?

I have an application that connects to multiple server. where one server will have ID that are foreign key to a table that is located on a different server. The issue here is that MySQL does not support linked servers so I can't run a left query…
Jaylen
  • 39,043
  • 40
  • 128
  • 221
4
votes
2 answers

Merging multiple arrays evenly and continuously

Imagine having a few array structures (I present three, but I'd like to be as flexible as possible) $array1 = array("red", "red", "red"); $array2 = array("green", "green", "green", "green"); $array3 = array("blue", "blue"); I need to merge…
Jorg Ancrath
  • 1,447
  • 10
  • 34
  • 61
4
votes
1 answer

2 query in an array using my code

I use a slider for my Wordpress featured articles. It selects a custom category and returns a set amount of posts. How do I make the first post that is displayed to be a custom one? Can I add an ID of a specific post directly in the slider code to…
user1649057
4
votes
2 answers

PHP array_merge key order

Does it matter in what order the keys in an array array doing an array_merge, i.e. would the keys in the second array below override the keys in the first array: array1 = array('username' => 'abc', 'level' => 'admin', 'status' => 'active'); array2 =…
Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
4
votes
1 answer

PHP array merging issue

I'm sure there have been many questions like these in the past, so sorry if it's came up before. Basically, i'm trying to merge two multidimensional arrays whilst not creating 2 keys for any duplicate keys. Here's an example: $one = array( 'foo'…
daryl
  • 14,307
  • 21
  • 67
  • 92