2015-12-28 284 views
0

我有这两个数组:PHP插入多维数组到另一个多维数组

Array 
(
    [InterfacedaRequisicaodePagamento] => Array 
     (
      [0] => Array 
       (
        [SequenciadoRegistro] => 15015 
        [CodigodaContadoDocumento] => 
       ) 

     ) 

) 

Array 
(
    [InterfaceGrupoRequisicaodePagamento] => Array 
     (
      [0] => Array 
       (
        [CodigodoProjeto] => 
       ) 

     ) 

) 

我需要的是的CodigodaContadoDocumento项之后插入第二阵列第一个数组生成一个JSON字符串,但array_push不起作用,并且在这种情况下我不知道如何使用array_splice

我使用

array_push($interfaceRequisicaoPagamento, $interfaceGrupoRequisicaodePagamento); 

,结果如下:

Array (
    [InterfacedaRequisicaodePagamento] => Array (
     [0] => Array (
      [SequenciadoRegistro] => 15015 
      [CodigodaContadoDocumento] => 
     ) 
    ) 
    [0] => Array (
     [InterfaceGrupoRequisicaodePagamento] => Array (
      [0] => Array (
       [CodigodoProjeto] => 
      ) 
     ) 
    ) 
) 

但我需要的是:

Array 
(
    [InterfacedaRequisicaodePagamento] => Array 
    (
     [0] => Array 
      (
       [SequenciadoRegistro] => 15015 
       [CodigodaContadoDocumento] => 
       [InterfaceGrupoRequisicaodePagamento] => Array 
       (
        [0] => Array 
         (
          [CodigodoProjeto] => 
         ) 

       ) 
      ) 
    ) 
) 
+0

你能发布您的代码? 'array_push()'应该工作得很好。 – jeroen

+2

请将预期结果添加到您的问题中。 (为什么没有人发布可复制数组?XD) – FirstOne

+0

@jeroen我使用'array_push($ interfaceRequisicaoPagamento,$ interfaceGrupoRequisicaodePagamento);',并将结果如下: \t阵列 \t( \t \t [InterfacedaRequisicaodePagamento] =>数组 \t \t \t( \t \t \t \t [0] =>数组 \t \t \t \t \t( \t \t \t \t \t \t [SequenciadoRegistro] => 15015 \t \t \t \t \t \t [CodigodaContadoDocumento] => \t \t \t \t \t) \t \t \t) \t \t [0] =>数组 \t \t \t( \t \t \t \t [InterfaceGrupoRequisicaodePagamento] =>数组 \t \t \t \t \t( \t \t \t \t \t \t [0] =>数组 \t \t \t \t \t \t \t( \t \t \t \t \t \t \t \t [CodigodoProjeto] => \t \t \t \t \t \t \t) \t \t \t \t \t) \t \t \t) \t) –

回答

1

,可以工作,以及:

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

试试吧。

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

?> 

=>输出

Array 
(
    [InterfacedaRequisicaodePagamento] => Array 
     (
      [0] => Array 
       (
        [SequenciadoRegistro] => 15015 
        [CodigodaContadoDocumento] => 
        [InterfaceGrupoRequisicaodePagamento] => Array 
         (
          [0] => Array 
           (
            [CodigodoProjeto] => 
           ) 

         ) 

       ) 

     ) 

) 
{"InterfacedaRequisicaodePagamento":[{"SequenciadoRegistro":15015,"CodigodaContadoDocumento":"","InterfaceGrupoRequisicaodePagamento":[{"CodigodoProjeto":""}]}]} 
+0

这一工程!谢谢! –