duanaiguang1960
duanaiguang1960
2018-04-25 22:53

当我遍历它时,php多维数组输出显示不正确的产品

已采纳

I generated an array within my function using this php code (part):

while (!$combined->EOF) {
        $orders_id = $combined->fields['orders_id'];
        $customers_name = $combined->fields['customers_name'];
        $products_name = $combined->fields['products_name'];
        $products_model = $combined->fields['products_model'];
        $products_quantity = $combined->fields['products_quantity'];

        $this_order_data = array(
                           'orders_id' => $orders_id,
                           'name' => $products_name,
                           'model' => $products_model,
                           'quantity' => $products_quantity,
                           );

        if (isset($this->timeframe[$id]['combined'][$oID])) {
            $this->timeframe[$id]['combined'][$oID]['products'][] = $this_order_data;
        } else {
            $this->timeframe[$id]['combined'][$oID]['order_id'] = $oID;
            $this->timeframe[$id]['combined'][$oID]['customer_name'] = $customers_name;  
            $this->timeframe[$id]['combined'][$oID]['products'][] = $this_order_data;
        }
$combined->MoveNext();
}

This gives an array like the following example:

[6] => Array
    (
        [order_id] => 1910
        [customer_name] => Customer A
        [products] => Array
            (
                [0] => Array
                    (
                        [orders_id] => 1910
                        [name] => Product 1
                        [model] => P1
                        [quantity] => 31
                    )

                [1] => Array
                    (
                        [orders_id] => 1910
                        [name] => Product 2
                        [model] => P2
                        [quantity] => 50
                    )

                [2] => Array
                    (
                        [orders_id] => 1910
                        [name] => Product 3
                        [model] => P3
                        [quantity] => 20
                    )

            )

    )

[7] => Array
    (
        [order_id] => 1911
        [customer_name] => Customer B
        [products] => Array
            (
                [0] => Array
                    (
                        [orders_id] => 1911
                        [name] => Product 2
                        [model] => P2
                        [quantity] => 75
                    )

                [1] => Array
                    (
                        [orders_id] => 1911
                        [name] => Product 4
                        [model] => P4
                        [quantity] => 30
                    )

            )

    )

I then use this code to output the code for display on screen

<?php
      $i = 0;
      foreach($timeframe['combined'] as $key => $c_data) if ($c_data['order_id'] == $c_data['products'][$i]['orders_id']){
      $items = count($c_data['products']);
?>
   <tr class="lineItemRow" <?php echo $rollover; ?>>
                        <td class="lineItemContent" align="left"><?php echo $c_data['products'][$i]['name']; ?></td>
   <td class="lineItemContent" align="left"><?php echo $c_data['products'][$i]['model']; ?></td>
   <td class="lineItemContent" align="right"><?php echo $c_data['products'][$i]['quantity']; ?></td>
   <td class="lineItemContent" align="right"><?php echo $c_data['products'][$i]['count']; ?></td>
   </tr>
  <?php 
  if ($i == $items){
      $i= 0;
  }else{
      $i=$i+1;
  }
}

Because I don't know how many products are going to be in each array, I assumed that I needed to get the count of $c_data['products'], set $i to 0 before the loop starts, and then increment it each time until it reached the value of the count. Only it doesn't work like that. What I've noticed is that $i is only incremented each time it moves to a new order, not each new product. This obviously gives me incorrect data because $i is not looking at the correct part of the array each time.

How should I correctly loop through each item in the 'products' array?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongqianchi0512 dongqianchi0512 3年前

    Why you don't loop into $c_data['products'] ?

    <?php
    
    foreach($timeframe['combined'] as $key => $c_data)
        foreach($c_data as $data)
           foreach($data['products'] as $product)
              if ($data['order_id'] == $product['orders_id']) { ?>
    
            <tr class="lineItemRow" <?php echo $rollover; ?>>
                <td class="lineItemContent" align="left"><?php echo 
                $product['name']; ?></td>
                <td class="lineItemContent" align="left"><?php echo 
                $product['model']; ?></td>
                <td class="lineItemContent" align="right"><?php echo 
                $product['quantity']; ?></td>
                <td class="lineItemContent" align="right"><?php echo 
                $product['count']; ?></td>
             </tr>
    
    <?php } ?>
    
    点赞 评论 复制链接分享

相关推荐