0

Here is an example of my data:

[204] => Array
    (
        [1] => Array
            (
                [leads] => 9
            )

        [2] => Array
            (
                [leads] => 15
            )

    )

[200] => Array
    (
        [1] => Array
            (
                [leads] => 7
            )

        [2] => Array
            (
                [leads] => 16
            )

        [3] => Array
            (
                [leads] => 5
            )

    )

I am at the stage where I trying to output the array data into a table but how do I set up the table headers dynamically so that the columns will be 1 | 2 | 3, even if some subsets don't have an array of that type?

The array is constructed from the results of a database query like so:

$dailytotals[$store][$campaigntypeid] = array('leads'=> $leads);

I tried a for each but just realised that it wouldn't work since not all subsets have all columns.

Is there a way to get what I am trying to find?

1 Answer 1

1

Try this

$columns = array();
foreach ($your_array as $key=> $arr) {
  $columns = array_unique(array_merge($columns, array_keys($arr)));
}

Another way

$columns = array_reduce($your_array, 
  function ($r, $val) { 
    return array_unique(array_merge($r, array_keys($val))); 
  }, 
  array()
);
Sign up to request clarification or add additional context in comments.

Comments

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.