I have the following array:
Array
(
[ID] => 2
[TokenPersona] => 54N65J4K7B6H9B76N9L67KJ54J5VN3B
[Contenido] => POP DAT!
[Date] => 2016-11-27 11:57:11
[tipoPrivacidad] => 1
[ControlEdad] => 0
)
And I have the array that I want to add:
Array
(
[ID] => 3
[TokenPersona] => 54N65J4K7B6H9B76N9L67KJ54J5VN3B
[Contenido] => Hey dot :v
[Date] => 2016-11-27 12:13:15
[tipoPrivacidad] => 1
[ControlEdad] => 0
)
For this I use:
$var = array_merge($array1,$array2);
But its result is:
Array
(
[ID] => 3
[TokenPersona] => 54N65J4K7B6H9B76N9L67KJ54J5VN3B
[Contenido] => Hey dot :v
[Date] => 2016-11-27 12:13:15
[tipoPrivacidad] => 1
[ControlEdad] => 0
)
And I would like the following result:
Array
(
[0] => Array
(
[ID] => 2
[TokenPersona] => 54N65J4K7B6H9B76N9L67KJ54J5VN3B
[Contenido] => POP DAT!
[Date] => 2016-11-27 11:57:11
[tipoPrivacidad] => 1
[ControlEdad] => 0
)
[1] => Array
(
[ID] => 3
[TokenPersona] => 54N65J4K7B6H9B76N9L67KJ54J5VN3B
[Contenido] => Hey dot :v
[Date] => 2016-11-27 12:13:15
[tipoPrivacidad] => 1
[ControlEdad] => 0
)
)
What am I doing wrong?
In response to the comment made by Error404
I don't use array_merge_recursive because it would return the following:
Array
(
[ID] => Array
(
[0] => 2
[1] => 3
)
[TokenPersona] => Array
(
[0] => 54N65J4K7B6H9B76N9L67KJ54J5VN3B
[1] => 54N65J4K7B6H9B76N9L67KJ54J5VN3B
)
[Contenido] => Array
(
[0] => POP DAT!
[1] => Hey dot :v
)
[Date] => Array
(
[0] => 2016-11-27 11:57:11
[1] => 2016-11-27 12:13:15
)
[tipoPrivacidad] => Array
(
[0] => 1
[1] => 1
)
[ControlEdad] => Array
(
[0] => 0
[1] => 0
)
)
And this is not how I want the result.
What you can do is directly add each of the arrays to a third array. For example, taking these two arrays as reference:
You can assign each of the arrays to a third array as follows:
Therefore, doing so
print_r($array3)
will give you the following result:Yes you can use the function you
array_merge()
just have to pack eacharray
in aarray
:We can create a simple function
array_merge_new()
:See Demos