Reputation: 61
I have this two arrays:
Array
(
[InterfacedaRequisicaodePagamento] => Array
(
[0] => Array
(
[SequenciadoRegistro] => 15015
[CodigodaContadoDocumento] =>
)
)
)
and
Array
(
[InterfaceGrupoRequisicaodePagamento] => Array
(
[0] => Array
(
[CodigodoProjeto] =>
)
)
)
What I need is to insert the second array after the CodigodaContadoDocumento
item of the first array to make a JSON string, but array_push
don't work for it, and I don't know how to use array_splice
in this case.
I'm using
array_push($interfaceRequisicaoPagamento, $interfaceGrupoRequisicaodePagamento);
and the result is the following:
Array (
[InterfacedaRequisicaodePagamento] => Array (
[0] => Array (
[SequenciadoRegistro] => 15015
[CodigodaContadoDocumento] =>
)
)
[0] => Array (
[InterfaceGrupoRequisicaodePagamento] => Array (
[0] => Array (
[CodigodoProjeto] =>
)
)
)
)
But what I need is:
Array
(
[InterfacedaRequisicaodePagamento] => Array
(
[0] => Array
(
[SequenciadoRegistro] => 15015
[CodigodaContadoDocumento] =>
[InterfaceGrupoRequisicaodePagamento] => Array
(
[0] => Array
(
[CodigodoProjeto] =>
)
)
)
)
)
Upvotes: 0
Views: 47
Reputation: 1455
Try it.
<?php
$array1 = array('InterfacedaRequisicaodePagamento' => array
( 0 => array
(
'SequenciadoRegistro' => 15015,
'CodigodaContadoDocumento' => ''
) ) );
$array2 = array('InterfaceGrupoRequisicaodePagamento' => array
(0 => array
(
'CodigodoProjeto' => ''
)));
$array1['InterfacedaRequisicaodePagamento']['0']['InterfaceGrupoRequisicaodePagamento'] = $array2['InterfaceGrupoRequisicaodePagamento'];
echo "<pre>";
print_r($array1);
$jsonData = json_encode($array1);
echo $jsonData;
?>
=> OUTPUT
Array
(
[InterfacedaRequisicaodePagamento] => Array
(
[0] => Array
(
[SequenciadoRegistro] => 15015
[CodigodaContadoDocumento] =>
[InterfaceGrupoRequisicaodePagamento] => Array
(
[0] => Array
(
[CodigodoProjeto] =>
)
)
)
)
)
{"InterfacedaRequisicaodePagamento":[{"SequenciadoRegistro":15015,"CodigodaContadoDocumento":"","InterfaceGrupoRequisicaodePagamento":[{"CodigodoProjeto":""}]}]}
Upvotes: 3
Reputation: 3884
That can work as well:
<?php
$array1 = array('InterfacedaRequisicaodePagamento' => array(array('SequenciadoRegistro' => 15015, 'CodigodaContadoDocumento' => null)));
$array2 = array('InterfaceGrupoRequisicaodePagamento' => array(array('CodigodoProjeto' => null)));
print_r($array1);
print_r($array2);
$array1['InterfaceGrupoRequisicaodePagamento'] = $array2['InterfaceGrupoRequisicaodePagamento'];
print_r($array1);
Upvotes: 1