Multidimensional array union in PHP

0

I have the following array:

Array
(
    [0] => Array
        (
            [chamado_codigo] => 5
            [representante_id] => 1
            [data] => 2015-10-08
            [razaoSocial] => mineoro
            [cliente_cidade] => chapeco
            [tecnico_nome] => jacomasio
        )

    [1] => Array
        (
            [chamado_codigo] => 6
            [representante_id] => 2
            [data] => 2015-10-08
            [razaoSocial] => mineoro
            [cliente_cidade] => chapeco
            [tecnico_nome] => Eduardo
        )

)

I want to join the array below with the above:

Array
(
    [0] => Array
        (
            [valor] => R$ 0,45
        )

    [1] => Array
        (
            [valor] => R$ 0,50
        )

)

I mean, I want to leave it that way, is it possible? How do I do it?

Array
    (
        [0] => Array
            (
                [chamado_codigo] => 5
                [representante_id] => 1
                [data] => 2015-10-08
                [razaoSocial] => mineoro
                [cliente_cidade] => chapeco
                [tecnico_nome] => jacomasio
                [valor] => R$ 0,45
            )

        [1] => Array
            (
                [chamado_codigo] => 6
                [representante_id] => 2
                [data] => 2015-10-08
                [razaoSocial] => mineoro
                [cliente_cidade] => chapeco
                [tecnico_nome] => Eduardo
                [valor] => R$ 0,50
            )

    )
    
asked by anonymous 14.10.2015 / 04:16

2 answers

1

You can try this way (by combining% of the original array and% of the secondary array):

$i=0;
$novoArray = array();
foreach($a as $valor) {
    $novoArray[] = array_merge($valor, $b[$i]);
    $i++;
}

See it working here: link

    
14.10.2015 / 09:15
0

Apparently the array_merge_recursive function should do the job.

However, when indexes are not defined or are numbers, the function does not work.

On the array_merge_recursive page, there is a function that worked well.

see example:

function my_array_merge ($arr,$ins) {
  if(is_array($arr))
  {
    if(is_array($ins)) foreach($ins as $k=>$v)
    {
      if(isset($arr[$k])&&is_array($v)&&is_array($arr[$k]))
      {
        $arr[$k] = my_array_merge($arr[$k],$v);
      }
      else {
// This is the new loop :)
        while (isset($arr[$k]))
          $k++;
        $arr[$k] = $v;
      }
    }
  }
  elseif(!is_array($arr)&&(strlen($arr)==0||$arr==0))
  {
    $arr=$ins;
  }
  return($arr);
}

//aproveitando o exemplo do nosso amigo Jorge Campos
$a = array( array("chamado_codigo"=>5,"representante_id"=>1), array("chamado_codigo"=>6,"representante_id"=>2) );
$b = array( array("valor"=>"R$ 0,45"), array("valor"=>"R$ 0,50") );

print_r(my_array_merge($a,$b));

which will result in

Array
(
    [0] => Array
        (
            [chamado_codigo] => 5
            [representante_id] => 1
            [valor] => R$ 0,45
        )

    [1] => Array
        (
            [chamado_codigo] => 6
            [representante_id] => 2
            [valor] => R$ 0,50
        )

)
    
14.10.2015 / 15:56