0

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

4
  • If your rows, are names identically to your array-keys, then it should be fairly easy, otherwise it will required much work Commented Apr 23, 2014 at 23:19
  • they are. would the process be to get a unique list of columns and rows, and then nest a bunch of loops? that seems like a clunky solution to me... Commented Apr 23, 2014 at 23:28
  • That or you could create a YAML like tree-syntax that will do the job done for you. It's obviously easier said than done Commented Apr 23, 2014 at 23:31
  • thank you. i don't mind a clunky solution as long as it's appropriate (and won't embarrass me during a code review!) Commented Apr 23, 2014 at 23:36

1 Answer 1

0

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>
Sign up to request clarification or add additional context in comments.

1 Comment

this says "avoid comments like thanks" but i'm gonna ignore that. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.