2017-11-10 1 views
3

wie zwei Arrays innerhalb ihrer jeweiligen Schlüssel zu kombinieren ich versucht Array_merge aber es hat nicht funktioniert, wie ich will. Ich habe auch versucht mit array_merge_recursive ... gleichen ...Wie verschmelzen zwei Arrays nach ihrem jeweiligen Schlüssel?

das sind meine beiden 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" 
    } 
    } 
} 

Wie kann ich Dies erreichen?

Ich bin nicht sehr gut darin zu erklären, warum ich grafisch darstellen entschieden:

enter image description here

Wenn Sie etwas anderes fragen Sie mich bitte benötigen!

+1

Das erste Array eine mehr Tiefe. Warum das? –

+0

Sorry, ich habe bearbeitet ... –

+0

Sie sollten wirklich die gewünschte Ausgabe im Array-Format bereitstellen. Das Bild zeigt nicht wirklich, wie die Zusammenführung erfolgen soll. – trincot

Antwort

1

könnte die folgende Lösung arbeiten:

/*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 */ 

Beispieldaten:

$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',),),),) 
1

Die Sache mit array_merge ist, dass es numerische Tasten anders als nicht-numerische Tasten zu behandeln. Der Operator + behandelt sie nicht anders, sondern nimmt den Wert aus dem ersten Array, wenn beide den gleichen Schlüssel haben.

So was auch immer Sie wollen.

könnten Sie diese Funktion verwenden, die wie array_merge_recursive funktionieren werden, aber nicht numerische Tasten unterschiedlich behandeln:

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; 
} 
+0

Sehr interessante Lösung, aber ich habe andere Ergebnisse mit den Beispieldaten in meiner Antwort hier. Ich frage mich, was das Problem sein könnte, das das verursacht. –

+0

Ja, ich hatte einen Kommentar zu der Lösung geschrieben, wie man die beiden Arrays für die Beispieldaten bekommt. –

+0

Danke für den Vorschlag 'var_export'. Aber ich habe mich bei @John B. Walugembe auf meine Antwort bezogen und versucht, die Ergebnisse zu vergleichen. Ich habe den Notizkommentar entfernt und stattdessen die Beispieldaten mit dem 'var_export' bearbeitet, um die Arrays deutlicher darzustellen. Der Unterschied, den ich suche, kann man unter folgendem Link sehen: http://algowriter.com/john/test_arr_merge.php –

Verwandte Themen