How to join two array

Asked

Viewed 517 times

2

need to join two array first array

array (size=11)
  7100 => boolean false
  7108 => boolean false
  7110 => boolean false
  7120 => boolean false
  7130 => boolean false
  7140 => boolean false
  7150 => boolean false
  7160 => boolean false
  7170 => boolean false
  7184 => boolean false
  7197 => boolean false

second array

array (size=11)
  7100 => 
    object(stdClass)[101]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7108 => 
    object(stdClass)[102]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7110 => 
    object(stdClass)[103]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7120 => 
    object(stdClass)[104]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7130 => 
    object(stdClass)[105]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7140 => 
    object(stdClass)[106]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7150 => 
    object(stdClass)[107]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7160 => 
    object(stdClass)[108]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7170 => 
    object(stdClass)[109]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7184 => 
    object(stdClass)[110]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
  7197 => 
    object(stdClass)[111]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0

matching would look like this.

example with size 1

7100 => 
    object(stdClass)[101]
      public 'qtdadulto' => int 0
      public 'qtdpintoumdia' => int 0
      public 'qtdovosferteis' => int 0
      (aqui a combinacao) public 'nome' => boolean false

My attempts

1º $combine = array_merge_recursive( $arResultado,$arraySaldo);
2º $arraycombine = array_combine($arResultado,$arraySaldo);

2 answers

5


You have objects you can cast from Stdclass to a simple array using

$arr = (array) $lista;

I believe the simplest way is to foreach and check if the key of the first array ($arr) exists in the second, if yes creates a new key called nome in $arr.

Example array - ideone

<?php

$arr = [
        7100 => [
                    'qtdadulto' =>  0,
                    'qtdpintoumdia' => 0,
                    'qtdovosferteis' =>0
                ],
        7108 => [
                    'qtdadulto' => 0,
                    'qtdpintoumdia' =>0,
                    'qtdovosferteis' => 0
                ],
        7110 => [
                    'qtdadulto' => 0,
                    'qtdpintoumdia' => 0,
                    'qtdovosferteis' =>  0
                    ]
];

$arr2 = [7100 => 1, 7108 => 200, 7110 => 500, 800 => 99];


foreach($arr as $k => $v){
    if(isset($arr2[$k])){
        $arr[$k]['nome'] = $arr2[$k];
    }
}

Exit:

Array
(
    [7100] => Array
        (
            [qtdadulto] => 0
            [qtdpintoumdia] => 0
            [qtdovosferteis] => 0
            [nome] => 1
        )

    [7108] => Array
        (
            [qtdadulto] => 0
            [qtdpintoumdia] => 0
            [qtdovosferteis] => 0
            [nome] => 200
        )

    [7110] => Array
        (
            [qtdadulto] => 0
            [qtdpintoumdia] => 0
            [qtdovosferteis] => 0
            [nome] => 500
        )

)
  • I ended up doing the foreach anyway.

  • There is the array_map that solves these cases

1

You can use the array_map, it applies the callback function for each element of the Ex array:

$resultMerge = array_map(function($a,$b){ $b->nome = $a; return $b; },$array1,$array2);

If you want to make the code more readable, you can do it:

function setItemArray($a, $b){ 
    $b->nome = $a; 
    return $b;
}

$resultMerge = array_map("setItemArray",$array1,$array2); 

Or call the direct array_map, since the array passed as parameter is changed.

 array_map("setItemArray",$array1,$array2);

Reference: array_map PHP Manual

Browser other questions tagged

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