0

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

                )
            )
    )
)
5
  • Can you post your code? array_push() should work just fine. Commented Dec 28, 2015 at 13:47
  • 2
    Please, add the expected outcome to your question. (Why don't people post copy-able arrays? xD) Commented Dec 28, 2015 at 13:48
  • @jeroen 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] => ) ) ) ) Commented Dec 28, 2015 at 13:55
  • Info added to the question. And what is the expected result? Commented Dec 28, 2015 at 13:58
  • @FirstOne The expected result is: Array ( [InterfacedaRequisicaodePagamento] => Array ( [0] => Array ( [SequenciadoRegistro] => 15015 [CodigodaContadoDocumento] => [InterfaceGrupoRequisicaodePagamento] => Array ( [0] => Array ( [CodigodoProjeto] => ) ) ) ) ) Commented Dec 28, 2015 at 13:59

2 Answers 2

3

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":""}]}]}
Sign up to request clarification or add additional context in comments.

Comments

1

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

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.