如何使用foreach

时间:2017-01-25 13:08:37

标签: php

我们有一个数组数据。

Array
(
    [39] => Array
        (
            [20170125] => Array
                (
                    [1000] => Array
                        (
                            [click] => 1129
                            [AFF_NAME] => EMAIL[1000]
                            [AFF_ACTIVATION] => 1
                            [CR_PER_CLICK_AFF] => 0%
                        )

                [1001] => Array
                    (
                        [click] => 59
                        [AFF_NAME] => SMS_Promotions[1001]
                        [AFF_ACTIVATION] => 0
                        [CR_PER_CLICK_AFF] => 0%
                    )
    )

[20170124] => Array
            (
                [1000] => Array
                    (
                        [click] => 1129
                        [AFF_NAME] => EMAIL[1000]
                        [AFF_ACTIVATION] => 1
                        [CR_PER_CLICK_AFF] => 0%
                    )

                [1001] => Array
                    (
                        [click] => 59
                        [AFF_NAME] => SMS_Promotions[1001]
                        [AFF_ACTIVATION] => 0
                        [CR_PER_CLICK_AFF] => 0%
                    )
    )

)
[56] => Array
        (
            [20170125] => Array
                (
                    [1000] => Array
                        (
                            [click] => 1129
                            [AFF_NAME] => EMAIL[1000]
                            [AFF_ACTIVATION] => 1
                            [CR_PER_CLICK_AFF] => 0%
                        )

                    [1001] => Array
                        (
                            [click] => 59
                            [AFF_NAME] => SMS_Promotions[1001]
                            [AFF_ACTIVATION] => 0
                            [CR_PER_CLICK_AFF] => 0%
                        )
        )
 [20170124] => Array
            (
                [1000] => Array
                    (
                        [click] => 1129
                        [AFF_NAME] => EMAIL[1000]
                        [AFF_ACTIVATION] => 1
                        [CR_PER_CLICK_AFF] => 0%
                    )

                [1001] => Array
                    (
                        [click] => 59
                        [AFF_NAME] => SMS_Promotions[1001]
                        [AFF_ACTIVATION] => 0
                        [CR_PER_CLICK_AFF] => 0%
                    )
    )

)

我想使用php foreach以下表格式显示这个数组,有人可以帮忙吗?

  

| -------- | --------------- | --------------------- --- | -------------------------- | --------------- | --- --------------------- | -------------------------- |       | AFF_NAME | 20170125(点击)| 20170125(AFF_ACTIVATION)| 20170125(CR_PER_CLICK_AFF)| 20170124(点击)| 20170124(AFF_ACTIVATION)| 20170124(CR_PER_CLICK_AFF)|       | -------- | --------------- | ------------------------ | -------------------------- | --------------- | ------ ------------------ | -------------------------- |       | EMAIL(1000)| 1129 | 1 | 0%| 59 | 0 | 0%|       | -------- | --------------- | ------------------------ | -------------------------- | --------------- | ------ ------------------ | -------------------------- |       | SMS_Promotions(1001)| 1129 | 1 | 0%| 59 | 0 | 0%|       | -------- | --------------- | ------------------------ | -------------------------- | --------------- | ------ ------------------ | -------------------------- |

2 个答案:

答案 0 :(得分:0)

每次使用2

  <?php  foreach ($array1 as $arrray =>$value){
                 foreach ($value as $abc => $value2){ ?>
      <tr>
  <td> <?php echo $value2['click']; ?></td>
        <td> all otheres</td>
  </tr>
  <?php  }
    } ?>

答案 1 :(得分:0)

罗马的一种方式......

$collector=array();
//flatten your array struct,  take datetime from first level and id from second
foreach($array as $date => $set){
  foreach($set as $aff_id => $subset){
    //bind datetime
    $subset['DATE'] = $date;
    //bind id
    $subset['AFF_ID'] = $aff_id;
    //sort by key to generate an valid table later
    ksort($subset);
    //collect it
    $collector[]=$subset;
  }
}

//create header
$table = "<table><tr><th>".implode('</th><th>',array_keys($collector[0]))."</th></tr>";
//create rows
foreach($collector as $set){
   $table .= "<tr><td>".implode('</td><td>',$set)."</td></tr>";
}
$table .= "</table>";
//output
print $table;

如果需要,请更改代码。