2017-03-02 3 views
0

Ich habe 2 Tabellen, "header" und "mach":Erstellen von Array von Schleifen

Header:

STATO |ID |CAMPIONATO 
Italy |5 |Serie A 
Spain |1 |Primera Division 
France|2 |Coppe de France 

mach:

STATO |home  |away  |scoreH|scoreA| 
Italy |juve  |Milan |0  |0  | 
Italy |Lazio |Roma  |0  |1  | 
Spain |Deportivo|Bilbao |1  |0  | 
Spain |A Madrid |Sevilla |1  |2  | 
France|Lille |Perigord |0  |0  | 

ich den folgenden PHP-Code verwenden, zu extrahieren Daten. Soll ich ein Array erstellen, die den Header enthält und die Gruppenspiele

$num=0; 
$mach=-1; 
foreach($first as $row2){ // cycling once 

      foreach($header as $row){ // cycles x times 

        ....  //Here recover the header data 
        $stato; 
        $id; 
        $campionato 



         for($i=0;$i<$count;++$i){ // in $count know how many times I have to do the cycle for each header (stato) 
         $mach=$mach+1; 
          ....   // Here recover the mach data 
          $home; 
          $away; 
          $scoreH; 
          $scoreA; 

         $info[$mach]= array('home'=>$home,'away'=>$away,'scoreH'=>$scoreH,'scoreA'=>$scoreA); 
         $groups[$num]=array($info[$mach]); 
         }//end for cycles 

         $headerArray[$num]=array('header'=>['stato'=>$stato,'id'=>$id,'campionato'=>$campionato],'mach'=>$groups[$um]); 
      $num++; 
      } //end header cycles 

         $blockArray[]=array('incontri'=>$headerArray); 

} //end first cycle 
         print_r($blockArray); 

Das Problem ist in der $ Gruppen, die alle Datum Spiel zurückkehrt. (Italien, Spanien, Frankreich) nicht nur Gruppen von Kopf. Wenn Sie diese Zeile PHP-Code:

array_push ($groups,$info[$mach]); 

statt:

$Groups[$num] = array($info[$mach]); 

stattdessen gibt nur das letzte Spiel (Zyklus) für jeden Header.

würde Ich mag dieses Array erstellen

['incontri'=> 
[ 
header=> 
    ['stato'=>Italy,'id'=>5,'campionato'=>Serie A], 

mach=> 
    ['home'=>Juve,'away'=>Milan,'scoreH'=>0,'scoreA'=>0], 
    ['home'=>Lazio,'away'=>Roma,'scoreH'=>0,'scoreA'=>0] 

], 
[....], 
] 

Was mache ich falsch?

Antwort

0

Ich kann keine geistige Zugkraft auf Ihrem Code bekommen, also habe ich es selbst umgeschrieben. Wenn ich Ihr Ziel verstehe, habe ich einen viel schlankeren, saubereren Prozess für die Erstellung Ihrer gewünschten multidimensionalen Array erstellt.

* Einige Dinge zu beachten:

  1. Stören Sie nicht Variablen deklarieren, wenn sie nur einmal verwendet werden; es sei denn, natürlich verbessert dies die Lesbarkeit wesentlich.
  2. Verwenden Sie verfügbare Array-Indizes innerhalb von Foreach-Schleifen, anstatt zusätzliche inkrementelle Variablen zu erstellen.
  3. $adeguata_mach_keys sucht alle Zeilen in $mach auf der Suche nach STATO Werte, die den Wert header STATO entsprechen, und gibt die Schlüssel zurück. Hier

ist der Code, den ich für den Erfolg getestet mit:

$first=array(); // only cycles once, so don't bother looping it, just call it by key (I am assuming 0, but that may be wrong) 
$first[0]=array(
    array("STATO"=>"Italy","ID"=>5,"CAMPIONATO"=>"Serie A"), 
    array("STATO"=>"Spain","ID"=>1,"CAMPIONATO"=>"Primera Division"), 
    array("STATO"=>"France","ID"=>1,"CAMPIONATO"=>"Coppe de France") 
); 
$mach=array(
    array("STATO"=>"Italy","home"=>"juve","away"=>"Milan","scoreH"=>"0","scoreA"=>"0"), 
    array("STATO"=>"Italy","home"=>"Lazio","away"=>"Roma","scoreH"=>"0","scoreA"=>"1"), 
    array("STATO"=>"Spain","home"=>"Deportivo","away"=>"Bilbao","scoreH"=>"1","scoreA"=>"0"), 
    array("STATO"=>"Spain","home"=>"A Madrid","away"=>"Sevilla","scoreH"=>"1","scoreA"=>"2"), 
    array("STATO"=>"Spain","home"=>"Lille","away"=>"Perigord","scoreH"=>"0","scoreA"=>"0") 
); 

foreach($first[0] as $header_index=>$header_row){ 
    foreach($header_row as $header_row_key=>$header_row_val){ 
     $result[$header_index]["header"][$header_row_key]=$header_row_val; 
     // identify which mach rows hold matching STATO values 
     $adeguata_mach_keys=array_keys(array_intersect(array_column($mach,"STATO"),array($header_row["STATO"]))); 
     foreach($adeguata_mach_keys as $mach_index=>$mach_key){ 
      foreach($mach[$mach_key] as $mach_row_key=>$mach_row_val){ 
       $result[$header_index]["mach"][$mach_index][$mach_row_key]=$mach_row_val; 
      } 
     } 
    } 
} 
$result=array("incontri"=>$result); // I wouldn't do this, but if it is necessary for your case, fine. 
print_r($result); 

Dies ist das Ergebnis (kein versehentliches Unterarray überschreiben):

array (
    'incontri' => array (
     0 => array (
      'header' => array (
       'STATO' => 'Italy', 
       'ID' => 5, 
       'CAMPIONATO' => 'Serie A', 
      ), 
      'mach' => array (
       0 => array (
        'STATO' => 'Italy', 
        'home' => 'juve', 
        'away' => 'Milan', 
        'scoreH' => '0', 
        'scoreA' => '0', 
       ), 
       1 => array (
        'STATO' => 'Italy', 
        'home' => 'Lazio', 
        'away' => 'Roma', 
        'scoreH' => '0', 
        'scoreA' => '1', 
       ), 
      ), 
     ), 
     1 => array (
      'header' => array (
       'STATO' => 'Spain', 
       'ID' => 1, 
       'CAMPIONATO' => 'Primera Division', 
      ), 
      'mach' => array (
       0 => array (
        'STATO' => 'Spain', 
        'home' => 'Deportivo', 
        'away' => 'Bilbao', 
        'scoreH' => '1', 
        'scoreA' => '0', 
       ), 
       1 => array (
        'STATO' => 'Spain', 
        'home' => 'A Madrid', 
        'away' => 'Sevilla', 
        'scoreH' => '1', 
        'scoreA' => '2', 
       ), 
      ), 
     ), 
     2 => array (
      'header' => array (
       'STATO' => 'France', 
       'ID' => 1, 
       'CAMPIONATO' => 'Coppe de France', 
      ), 
      'mach' => array (
       0 => array (
        'STATO' => 'France', 
        'home' => 'Lille', 
        'away' => 'Perigord', 
        'scoreH' => '0', 
        'scoreA' => '0', 
       ), 
      ), 
     ), 
    ), 
)