2016-10-25 1 views
0

Ich habe eine benutzerdefinierte Tabelle, die ich Abfrage und die Kombination von Transaktions-IDs, die gleich sind, dann platzieren die Abfrage in und Array. Auf diese Weise können die Produkte mit derselben Transaktions-ID kombiniert werden. Jetzt muss ich herausfinden, wie man mehr als ein Objekt von den Schlüsseln bekommt. Beispielcode:PHP Schleife durch Array, wo Schlüssel mehr als ein Objekt oder Array hat

Erstabfrage

$myArray = $wpdb->get_results("SELECT * FROM " tablename); 
$newArray=array();     
foreach($myArray as $val){ 
    $newKey=$val->txn_id; 
    $newArray[$newKey][]=$val; 
} 

dieses heraus Drucken gibt mir:

Array 
(
    [265] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 26 
        [txn_id] => 265 
        [product_id] => 99 
        [product_name] => Product name 
       ) 

      [1] => stdClass Object 
       (
        [id] => 27 
        [txn_id] => 265 
        [product_id] => 98 
        [variation_id] => 
        [product_name] => Product name 
       ) 

     ) 

    [244] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 28 
        [txn_id] => 244 
        [product_id] => 98 
        [product_name] => Product name 
       ) 

     ) 

    [299] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 42 
        [txn_id] => 299 
        [product_id] => 99 
        [product_name] => Product name 
       ) 

     ) 

    [300] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 43 
        [txn_id] => 300 
        [product_id] => 99 
        [product_name] => Product name 
       ) 

     ) 

) 

Mein Loop:

<?php 
foreach ($newArray as $key => $array){ 
     ?> 
<table border="0" cellpadding="0" cellspacing="0"> 
    <tr class="header"> 
     <td width="73%" valign="top">KEY</td> 
     <td width="73%" valign="top">Product Id</td> 
     <td width="27%" valign="top">Product Name</td> 
    </tr> 
    <tr class="details"> 
     <td width="27%" valign="top"><?php echo $key; ?></td> 
     <td width="73%" valign="top"><?php echo $newArray[$key][0]->product_id; ?></td> 
     <td width="27%" valign="top"><?php echo $newArray[$key][0]->product_name ?></td> 
    </tr> 
</table> 
<?php } ?> 

Das Problem damit ist für den Schlüssel "265" Ich bekomme nur das erste Objekt mit $ newArray [$ key] [0]; Ich muss beide bekommen.

Antwort

1

Sie müssen das innere Array auch wie unten beschrieben schleifen.

<?php 
foreach ($newArray as $key => $array){ 
     ?> 
<table border="0" cellpadding="0" cellspacing="0"> 
    <tr class="header"> 
     <td width="73%" valign="top">KEY</td> 
     <td width="73%" valign="top">Product Id</td> 
     <td width="27%" valign="top">Product Name</td> 
    </tr> 
     <?php 
     foreach($array as $values) { 
      ?> 
    <tr class="details"> 
     <td width="27%" valign="top"><?php echo $key; ?></td> 
     <td width="73%" valign="top"><?php echo $values->product_id; ?></td> 
     <td width="27%" valign="top"><?php echo $values->product_name ?></td> 
    </tr> 
     <?php } ?> 
</table> 
<?php } ?> 
+0

Danke, ich wusste, dass es etwas sein musste, das ich einfach vermisste. –

Verwandte Themen