PHP multidimensional array union

Asked

Viewed 261 times

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 at that, is it possible? How do I do?

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
            )

    )
  • have tried with array_merge() ? http://www.w3schools.com/php/func_array_merge.asp

2 answers

1

You can try it this way (by $a the original array and $b high school):

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

See working here: http://ideone.com/cLwZ2H

0

Apparently the function array_merge_recursive should do the job.

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

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

take the 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
        )

)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.