简体   繁体   中英

Creating a table from a multidimensional array in PHP

I'm receiving arrays from an API that have a variable number of fields and I'm trying to get it into a table.

A sample object looks like this:

array
  'silver' => 
    array
      'assets' => 
        array
          'Article' => float 2
          'ROS_Medium_1' => float 37704
          'ROS_Medium_2' => float 37711
          'ROS_Medium_3' => float 37546
          'ROS_Leaderboard_Footer' => float 37941
          'ROS_Leaderboard_Infinite Scroll' => float 3636
      'price' => float 375
      'discount' => int 0
      'banner_cpm' => float 12.79
      'banner_sov' => float 0.0099
      'content_cpm' => float 150
      'content_sov' => float 0.0028
      'fixed_cpm' => float 0
      'blended_cpm' => float 26.26
  'gold' => 
    array
      'assets' => 
        array
          'Article' => float 2
          'ROS_Leaderboard' => float 15212
          'ROS_CCF' => float 1
          'ROS_Halfpage' => float 15212
          'ROS_Medium_1' => float 12459
          'ROS_Medium_2' => float 12476
          'ROS_Medium_3' => float 12476
          'ROS_Leaderboard_Footer' => float 12390
          'ROS_Leaderboard_Infinite Scroll' => float 1239
      'price' => int 500
      'discount' => int 0
      'banner_cpm' => float 18.84
      'banner_sov' => float 0.008
      'content_cpm' => float 150
      'content_sov' => float 0.0028
      'fixed_cpm' => float 0
      'blended_cpm' => float 29.8

The number of columns equals the number of top level arrays (so, silver and gold here)

The number of top-level arrays can vary, and the assets sub-array can vary from array to array (notice how the "gold" array has more assets in the sample code). There should be a row for each asset, and each other key in the array.

What would be the cleanest way to get all this information into a table-ready format? As far as I can tell, the first step would be to make an array of all the unique assets. But then without clunkily nesting a bunch of foreach loops, I'm not sure where to go from here.

The desired output will look something like http://i.imgur.com/m0IJpUj.png

You could do this in numerous way, I would do it this way:

Sample Values:

$values_from_api = array(
'silver' => array(
    'assets' => array(
        'Article' => 2.0,
        'ROS_Medium_1' => 37704.0,
        'ROS_Medium_2' => 37711.0,
        'ROS_Medium_3' => 37546.0,
        'ROS_Leaderboard_Footer' => 37941.0,
        'ROS_Leaderboard_Infinite Scroll' => 3636.0,
        'price' => 375,
        'discount' => 0,
        'banner_cpm' => 12.79,
        'banner_sov' => 0.0099,
        'content_cpm' => 150.0,
        'content_sov' => 0.0028,
        'fixed_cpm' => 0.0,
        'blended_cpm' => 26.26,
  ),
),
'gold' => array(
    'assets' => array(
        'Article' => 2.0,
        'ROS_Leaderboard' => 15212.0,
        'ROS_CCF' => 1.0,
        'ROS_Halfpage' => 15212.0,
        'ROS_Medium_1' => 12459.0,
        'ROS_Medium_2' => 12476.0,
        'ROS_Medium_3' => 12476.0,
        'ROS_Leaderboard_Footer' => 12390.0,
        'ROS_Leaderboard_Infinite Scroll' => 1239.0,
        'price' => 500,
        'discount' => 0,
        'banner_cpm' => 18.84,
        'banner_sov' => 0.008,
        'content_cpm' => 150.0,
        'content_sov' => 0.0028,
        'fixed_cpm' => 0.0,
        'blended_cpm' => 29.8,
    ),
),
);

First you should take out all the necessary columns:

$left_columns = array();
foreach($values_from_api as $type_key => $type) {
    foreach($type['assets'] as $columns => $value) {
        $left_columns[] = $columns;
    }
}

$left_columns = array_unique($left_columns);

After you have gathered necessary columns, you could print them like this:

<table border="1" cellpadding="10">
    <thead>
        <th width="200"></th>
        <th>Silver</th>
        <th>Gold</th>
    </thead>
    <tbody>
    <?php foreach($left_columns as $key => $column_name): ?>
        <tr>
            <td><?php echo $column_name; ?></td>
            <?php
            foreach($values_from_api as $type_key => $type) {
                foreach($type as $columns => $value) {
                    if(isset($value[$column_name])) {
                        echo "<td>$value[$column_name]</td>";
                    } else {
                        echo "<td>N/A</td>";
                    }
                }
            }
            ?>
            </tr>
    <?php endforeach; ?>
    </tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM