2016-06-13 6 views
-1

Hallo Ich habe diese Art von Array,zwei Arrays Daten auf der gleichen ID in PHP-Kombination

Array 
(
    [1] => Array 
     (
      [0] => FGC00092 
      [splcode] => FGC00092 
      [1] => 2016-05-02,2016-05-09 
      [tran_date] => 2016-05-02,2016-05-09 
      [2] => 3,40 
      [unit] => 3,40 
      [3] => 300,320 
      [value] => 300,320 
     ) 

    [2] => Array 
     (
      [0] => FGC00085 
      [splcode] => FGC00085 
      [1] => 2016-05-06 
      [tran_date] => 2016-05-06 
      [2] => 2 
      [unit] => 2 
      [3] => 400 
      [value] => 400 
     ) 

    [3] => Array 
     (
      [0] => FGC00099 
      [splcode] => FGC00099 
      [1] => 2016-05-06 
      [tran_date] => 2016-05-06 
      [2] => 3 
      [unit] => 3 
      [3] => 600 
      [value] => 600 
     ) 

    [4] => Array 
     (
      [0] => FGC00092 
      [splcode] => FGC00092 
      [1] => 2016-05-09 
      [tran_date] => 2016-05-09 
      [2] => 40 
      [unit] => 40 
      [3] => 320 
      [value] => 320 
     ) 

    [5] => Array 
     (
      [0] => FGC00115 
      [splcode] => FGC00115 
      [1] => 2016-05-10 
      [tran_date] => 2016-05-10 
      [2] => 476 
      [unit] => 476 
      [3] => 3808 
      [value] => 3808 
     ) 

    [6] => Array 
     (
      [0] => FGC00062 
      [splcode] => FGC00062 
      [1] => 2016-05-25 
      [tran_date] => 2016-05-25 
      [2] => 10 
      [unit] => 10 
      [3] => 80 
      [value] => 80 
     ) 

    [7] => Array 
     (
      [0] => FGC00059 
      [splcode] => FGC00059 
      [1] => 2016-05-30 
      [tran_date] => 2016-05-30 
      [2] => 50 
      [unit] => 50 
      [3] => 400 
      [value] => 400 
     ) 

    [8] => Array 
     (
      [0] => FGC00117 
      [splcode] => FGC00117 
      [1] => 2016-05-31 
      [tran_date] => 2016-05-31 
      [2] => 50 
      [unit] => 50 
      [3] => 400 
      [value] => 400 
     ) 

) 

i die sie verbinden wollen, die gleiche ID haben. wie für FGC00092, Array-Struktur muss,

Array 
(
    [1] => Array 
     (
      [0] => FGC00092 
      [splcode] => FGC00092 
      [1] => 2016-05-02 
      [tran_date] => 2016-05-02 
      [2] => 3 
      [unit] => 3 
      [3] => 300 
      [value] => 300 
     ) 

    [2] => Array 
     (
      [0] => FGC00085 
      [splcode] => FGC00085 
      [1] => 2016-05-06 
      [tran_date] => 2016-05-06 
      [2] => 2 
      [unit] => 2 
      [3] => 400 
      [value] => 400 
     ) 

    [3] => Array 
     (
      [0] => FGC00099 
      [splcode] => FGC00099 
      [1] => 2016-05-06 
      [tran_date] => 2016-05-06 
      [2] => 3 
      [unit] => 3 
      [3] => 600 
      [value] => 600 
     ) 
    [4] => Array 
     (
      [0] => FGC00115 
      [splcode] => FGC00115 
      [1] => 2016-05-10 
      [tran_date] => 2016-05-10 
      [2] => 476 
      [unit] => 476 
      [3] => 3808 
      [value] => 3808 
     ) 

    [5] => Array 
     (
      [0] => FGC00062 
      [splcode] => FGC00062 
      [1] => 2016-05-25 
      [tran_date] => 2016-05-25 
      [2] => 10 
      [unit] => 10 
      [3] => 80 
      [value] => 80 
     ) 

    [6] => Array 
     (
      [0] => FGC00059 
      [splcode] => FGC00059 
      [1] => 2016-05-30 
      [tran_date] => 2016-05-30 
      [2] => 50 
      [unit] => 50 
      [3] => 400 
      [value] => 400 
     ) 

    [7] => Array 
     (
      [0] => FGC00117 
      [splcode] => FGC00117 
      [1] => 2016-05-31 
      [tran_date] => 2016-05-31 
      [2] => 50 
      [unit] => 50 
      [3] => 400 
      [value] => 400 
     ) 

) 
+0

foreach loop verwenden und neues Array erstellen –

+0

PLZ einen Demo-Code für HINT – 789khan

+1

Sie müssen mehr was Sie wollen ??? Von Anfang an schlage ich vor, foreach zu verwenden. –

Antwort

1

Sie können foreach und in_array verwenden, um Ihr Problem zu lösen, wie unten.

$tempArray = $outArray = array(); 
$count = 1; 

foreach($yourArray as $aKey=>$aVal){ 
    if (in_array($aVal['splcode'], $aVal)){ 
     //do something 
     continue; // it will skip the current loop if the condition satisfies 
    } 
    $outArray[$count] = $aVal; 
    $tempArray[] = $aVal['splcode']; 
    $count++; 
} 
//You will get the result in the variable $outArray 
unset($tempArray); // clear the temp array 
+0

Ich mag Ihre Inline-Kommentare, sollte nicht $ count als Null initialisiert werden? – vivoconunxino

+0

@vivoconunxino, Keine Probleme. Wir können $ count als Null verwenden, falls erforderlich. –

-1

Die array_unique-Funktion wird dies für Sie tun. Sie mussten nur das Flag SORT_REGULAR hinzufügen:

$ items_thread = array_unique ($ items_thread, SORT_REGULAR);

+0

Es wird in diesem Fall nicht funktionieren, da die Werte unterschiedlich sind (obwohl sie die gleiche ID teilen) – vivoconunxino

Verwandte Themen