Tyler Robinson
Tyler Robinson

Reputation: 367

PHP loop through array where key has more than one object or array

I have a custom table that I am querying and combining transaction id's that are the same, then placing the query into and array. This way the products with the same transaction id can be combined together. Now, I need to figure out how to get more than one object from the keys. example code:

Initial query

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

Printing this out will give me:

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
                )

        )

)

My 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 } ?>

The problem with this is for the key "265" I only get the first object using $newArray[$key][0]; I need to get both.

Upvotes: 0

Views: 38

Answers (1)

Anandhan
Anandhan

Reputation: 697

You need to loop the inner array also as below.

<?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 } ?>

Upvotes: 1

Related Questions