how to merge two arrays according to their respective key?

Viewed 107

how to combine two arrays within their respective keys i tryied array_merge but it did not work as I want. I also tried with array_merge_recursive... the same...

these are my two arrays:

array 1 :

   array(2) {
  [264]=>
  array(6) {
    [0]=>
    array(5) {
      ["id"]=>
      string(2) "64"
      ["concepto"]=>
      string(27) "IIBB Contribuyentes Locales"
      ["impuesto"]=>
      string(10) "Anticipo10"
      ["agencia"]=>
      string(4) "ARBA"
      ["vencimiento_del_mes"]=>
      string(10) "2017-11-21"
    }
    [1]=>
    array(5) {
      ["id"]=>
      string(2) "74"
      ["concepto"]=>
      string(26) "IIBB convenio multilateral"
      ["impuesto"]=>
      string(10) "Anticipo10"
      ["agencia"]=>
      string(4) "ARBA"
      ["vencimiento_del_mes"]=>
      string(10) "2017-11-13"
    }
    [2]=>
    array(5) {
      ["id"]=>
      string(1) "1"
      ["concepto"]=>
      string(11) "recaudacion"
      ["impuesto"]=>
      string(4) "IIBB"
      ["agencia"]=>
      string(4) "AGIP"
      ["vencimiento_del_mes"]=>
      string(10) "2017-11-07"
    }
  }
  [265]=>
  array(14) {
    [0]=>
    array(5) {
      ["id"]=>
      string(2) "65"
      ["concepto"]=>
      string(27) "IIBB Contribuyentes Locales"
      ["impuesto"]=>
      string(10) "Anticipo10"
      ["agencia"]=>
      string(4) "ARBA"
      ["vencimiento_del_mes"]=>
      string(10) "2017-11-22"
    }
    [1]=>
    array(5) {
      ["id"]=>
      string(3) "101"
      ["concepto"]=>
      string(41) "Regimen General de Percepcion (Percibido)"
      ["impuesto"]=>
      string(24) "Segunda Quincena Octubre"
      ["agencia"]=>
      string(4) "ARBA"
      ["vencimiento_del_mes"]=>
      string(10) "2017-11-13"
    }
    [2]=>
    array(5) {
      ["id"]=>
      string(3) "101"
      ["concepto"]=>
      string(41) "Regimen General de Percepcion (Percibido)"
      ["impuesto"]=>
      string(26) "Primera Quincena Noviembre"
      ["agencia"]=>
      string(4) "ARBA"
      ["vencimiento_del_mes"]=>
      string(10) "2017-11-24"
    }
  }
}

array 2:

  array(2) {
  [264]=>
  array(9) {
    [0]=>
    array(6) {
      ["idImpuesto"]=>
      int(10)
      ["idConcepto"]=>
      int(19)
      ["periodo"]=>
      string(4) "2017"
      ["tipoOperacion"]=>
      string(12) "PRESENTACION"
      ["vencimiento"]=>
      string(10) "2017-11-13"
      ["formularios"]=>
      string(3) "713"
    }
    [1]=>
    array(5) {
      ["idImpuesto"]=>
      int(10)
      ["idConcepto"]=>
      int(19)
      ["periodo"]=>
      string(4) "2017"
      ["tipoOperacion"]=>
      string(4) "PAGO"
      ["vencimiento"]=>
      string(10) "2017-11-13"
    }
    [2]=>
    array(6) {
      ["idImpuesto"]=>
      int(30)
      ["idConcepto"]=>
      int(19)
      ["periodo"]=>
      string(7) "2017-10"
      ["tipoOperacion"]=>
      string(12) "PRESENTACION"
      ["vencimiento"]=>
      string(10) "2017-11-21"
      ["formularios"]=>
      string(8) "731,2002"
    }
  }
  [265]=>
  array(3) {
    [0]=>
    array(6) {
      ["idImpuesto"]=>
      int(30)
      ["idConcepto"]=>
      int(19)
      ["periodo"]=>
      string(7) "2017-10"
      ["tipoOperacion"]=>
      string(12) "PRESENTACION"
      ["vencimiento"]=>
      string(10) "2017-11-22"
      ["formularios"]=>
      string(8) "731,2002"
    }
    [1]=>
    array(5) {
      ["idImpuesto"]=>
      int(30)
      ["idConcepto"]=>
      int(19)
      ["periodo"]=>
      string(7) "2017-10"
      ["tipoOperacion"]=>
      string(4) "PAGO"
      ["vencimiento"]=>
      string(10) "2017-11-22"
    }
    [2]=>
    array(5) {
      ["idImpuesto"]=>
      int(308)
      ["idConcepto"]=>
      int(19)
      ["periodo"]=>
      string(7) "2017-10"
      ["tipoOperacion"]=>
      string(4) "PAGO"
      ["vencimiento"]=>
      string(10) "2017-11-06"
    }
  }
}

How can I achieve this?

I'm not very good at explaining why I decided to graph:

enter image description here

If you need something else please ask me!

2 Answers

The following solution could work:

/*Let $array1 and $array2 be the two arrays to be combined into one $array3 */  

/* First get the unique values for the keys to be combined: */
$arr = [];
foreach($array1 as $k => $v){ $arr[] = $k; }
foreach($array2 as $k => $v){ $arr[] = $k; }
$arr = array_unique($arr);

/* Scan the two arrays with the unique keys to get the required $array3: */
$array3 = [];
foreach($arr as $v){
if(array_key_exists($v, $array1)){ $array3[$v][] = $array1[$v]; }
if(array_key_exists($v, $array2)){ $array3[$v][] = $array2[$v]; }
}

var_export($array3);   /* the desired result */

Sample data:

$array1 = array ( 264 => array ( 0 => array ( 'id' => '64', 'concepto' => 'IIBB Contribuyentes Locales', 'impuesto' => 'Anticipo10', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-21', ), 1 => array ( 'id' => '74', 'concepto' => 'IIBB convenio multilateral', 'impuesto' => 'Anticipo10', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-13', ), 2 => array ( 'id' => '1', 'concepto' => 'recaudacion', 'impuesto' => 'IIBB', 'agencia' => 'AGIP', 'vencimiento_del_mes' => '2017-11-07', ), ), 265 => array ( 0 => array ( 'id' => '65', 'concepto' => 'IIBB Contribuyentes Locales', 'impuesto' => 'Anticipo10', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-22', ), 1 => array ( 'id' => '101', 'concepto' => 'Regimen General de Percepcion (Percibido)', 'impuesto' => 'Segunda Quincena Octubre', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-13', ), 2 => array ( 'id' => '101', 'concepto' => 'Regimen General de Percepcion (Percibido)', 'impuesto' => 'Primera Quincena Noviembre', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-24', ), ), ); 

$array2 = array (264 => array ( 0 => array ( 'idImpuesto' => 10, 'idConcepto' => 19, 'periodo' => '2017', 'tipoOperacion' => 'PRESENTACION', 'vencimiento' => '2017-11-13', 'formularios' => '713', ), 1 => array ( 'idImpuesto' => 10, 'idConcepto' => 19, 'periodo' => '2017', 'tipoOperacion' => 'PAGO', 'vencimiento' => '2017-11-13', ), 2 => array ( 'idImpuesto' => 30, 'idConcepto' => 19, 'periodo' => '2017-10', 'tipoOperacion' => 'PRESENTACION', 'vencimiento' => '2017-11-21', 'formularios' => '731,2002', ), ), 265 => array ( 0 => array ( 'idImpuesto' => 30, 'idConcepto' => 19, 'periodo' => '2017-10', 'tipoOperacion' => 'PRESENTACION', 'vencimiento' => '2017-11-22', 'formularios' => '731,2002', ), 1 => array ( 'idImpuesto' => 30, 'idConcepto' => 19, 'periodo' => '2017-10', 'tipoOperacion' => 'PAGO', 'vencimiento' => '2017-11-22', ), 2 => array ( 'idImpuesto' => 308, 'idConcepto' => 19, 'periodo' => '2017-10', 'tipoOperacion' => 'PAGO', 'vencimiento' => '2017-11-06', ), ), );

Result:
$array3 = array ( 264 => array ( 0 => array ( 0 => array ( 'id' => '64', 'concepto' => 'IIBB Contribuyentes Locales', 'impuesto' => 'Anticipo10', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-21', ), 1 => array ( 'id' => '74', 'concepto' => 'IIBB convenio multilateral', 'impuesto' => 'Anticipo10', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-13', ), 2 => array ( 'id' => '1', 'concepto' => 'recaudacion', 'impuesto' => 'IIBB', 'agencia' => 'AGIP', 'vencimiento_del_mes' => '2017-11-07', ), ), 1 => array ( 0 => array ( 'idImpuesto' => 10, 'idConcepto' => 19, 'periodo' => '2017', 'tipoOperacion' => 'PRESENTACION', 'vencimiento' => '2017-11-13', 'formularios' => '713', ), 1 => array ( 'idImpuesto' => 10, 'idConcepto' => 19, 'periodo' => '2017', 'tipoOperacion' => 'PAGO', 'vencimiento' => '2017-11-13', ), 2 => array ( 'idImpuesto' => 30, 'idConcepto' => 19, 'periodo' => '2017-10', 'tipoOperacion' => 'PRESENTACION', 'vencimiento' => '2017-11-21', 'formularios' => '731,2002', ), ), ), 265 => array ( 0 => array ( 0 => array ( 'id' => '65', 'concepto' => 'IIBB Contribuyentes Locales', 'impuesto' => 'Anticipo10', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-22', ), 1 => array ( 'id' => '101', 'concepto' => 'Regimen General de Percepcion (Percibido)', 'impuesto' => 'Segunda Quincena Octubre', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-13', ), 2 => array ( 'id' => '101', 'concepto' => 'Regimen General de Percepcion (Percibido)', 'impuesto' => 'Primera Quincena Noviembre', 'agencia' => 'ARBA', 'vencimiento_del_mes' => '2017-11-24', ), ), 1 => array ( 0 => array ( 'idImpuesto' => 30, 'idConcepto' => 19, 'periodo' => '2017-10', 'tipoOperacion' => 'PRESENTACION', 'vencimiento' => '2017-11-22', 'formularios' => '731,2002', ), 1 => array ( 'idImpuesto' => 30, 'idConcepto' => 19, 'periodo' => '2017-10', 'tipoOperacion' => 'PAGO', 'vencimiento' => '2017-11-22', ), 2 => array ( 'idImpuesto' => 308, 'idConcepto' => 19, 'periodo' => '2017-10', 'tipoOperacion' => 'PAGO', 'vencimiento' => '2017-11-06', ), ), ), )

The thing with array_merge is that it will treat numerical keys differently from non-numerical keys. The + operator will not treat them differently, but takes the value from the first array when both have the same key.

So neither does what you want.

You could use this function, which will work like array_merge_recursive, but will not treat numerical keys differently:

function mergeByKeys($a, $b) {
    if (!is_array($a) || !is_array($b)) return $a;
    foreach (array_flip(array_keys($a))+array_flip(array_keys($b)) as $key => $_) {
        $results[$key] = !isset($a[$key]) ? $b[$key]
                       : (!isset($b[$key]) ? $a[$key] 
                          : mergeByKeys($a[$key], $b[$key]));
    }
    return $results;
}
Related