PHP循环遍历数组,其中key具有多个对象或数组

时间:2016-10-25 08:49:39

标签: arrays foreach key

我有一个自定义表,我正在查询并组合相同的事务ID,然后将查询放入和数组。这样,具有相同交易ID的产品可以组合在一起。现在,我需要弄清楚如何从键中获取多个对象。示例代码:

初始查询

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

打印出来会给我:

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

问题在于密钥&#34; 265&#34;我只使用$ newArray [$ key] [0]得到第一个对象;我需要得到两者。

1 个答案:

答案 0 :(得分:1)

您还需要循环内部数组,如下所示。

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