简体   繁体   中英

PHP Insert multi-dimensional array into another multi-dimensional array

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] =>  
                        )

                )
            )
    )
)

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":""}]}]}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM