0

I want to be able to display the multidimensional associative array in a table. The arrays are created by Solarium API which is used for debugging any indexing issues. Each array has different number of arrays and keys.

I want it keep it in a way that it works with any number or arrays and keys. I started with using a foreach loop but I'm stuck at this point. How would I go about doing this?

Code I have so far:

foreach ($metadatas as $metadata) {
    foreach($metadata as $type => $data) {
        echo '<tr>';
            echo '<td>'.$type.'</td>';
            echo '<td>'.$data.'</td>';
        echo '</tr>';
    }
}

This is the array I get using print_r():

Solarium\QueryType\Extract\Query Object
(
    [options:protected] => Array
        (
            [handler] => update/extract
            [resultclass] => Solarium\QueryType\Extract\Result
            [documentclass] => Solarium\QueryType\Update\Query\Document\Document
            [omitheader] => 
            [extractonly] => 
            [uprefix] => ignored_
            [commit] => 1
            [file] => http://url.com/branch/files/2015/03/Client-Feedback-Form.doc
            [document] => Solarium\QueryType\Update\Query\Document\Document Object
                (
                    [boost:protected] => 
                    [modifiers:protected] => Array
                        (
                        )
                    [key:protected] => 
                    [fieldBoosts:protected] => Array
                        (
                            [id] => 
                            [site] => 
                            [description] => 
                            [url] => 
                            [title] => 
                        )
                    [version:protected] => 
                    [helper:protected] => Solarium\Core\Query\Helper Object
                        (
                            [placeHolderPattern:protected] => /%(L|P|T|)([0-9]+)%/i
                            [assembleParts:protected] => 
                            [derefencedParamsLastKey:protected] => 0
                            [query:protected] => Solarium\QueryType\Update\Query\Document\Document Object
 *RECURSION*
                        )
                    [filterControlCharacters:protected] => 1
                    [fields:protected] => Array
                        (
                            [id] => 227-7653
                            [site] => Branch Name
                            [description] => 
                            [url] => http://url.ca/branch/files/2015/03/Client-Feedback-Form.doc
                            [title] => Client Feedback Form
                        )
                )
        )
    [fieldMappings:protected] => Array
        (
            [content_type] => type
            [author] => authors
            [last_modified] => lastModifiedDate
            [creation_date] => creationDate
            [content] => content
        )

    [helper:protected] => 
    [params:protected] => Array
        (
        )
)
Kumaran
  • 3
  • 3
  • it must be echoing something, if this array and subarrays have that content. Maybe you are trying it in .html file instead of .php file? – n-dru Mar 09 '15 at 20:19
  • @n-dru It's not echoing anything. The file is .php as it's a plugin for WordPress – Kumaran Mar 12 '15 at 15:31

2 Answers2

0

I need more fake internet points to comment. So instead you get my poor answer. I'd try some sort of recursive function calling.

function someFunction($table , $array){
  foreach($array as $key => $value){
      if(is_array($value)){
          someFunction(&$table, $value)
      }
      else {
          //Add to your existing $table
      }
  }
  return $table;
}
$table = someFunction("" , array());

obviously this is super simplistic view. But the idea is to keep passing your table deeper and deeper into the array. And eventually it will fall back out as you stop running into new arrays. I did something similar a while back passing around a DOMDocument() to build a super complex XML.

But this is only really useful when you don't know the possible size or depth of an array. If your array has keys, even if it's multidimensional, that you know will or won't exist and how deep they go. It's probably better to follow the answer in your comments and just build a nice HTML page.

Good luck.

Squeegy
  • 869
  • 9
  • 20
0

As you can see from the first line of your output, it is not an array, but an OBJECT! And as you see in [options:protected] , it is protected variable, so you cannot access it from external foreach loop. What you can do, is to declare a loop function inside that class:

class Query{
    ....
    ....
    public function iterate(){
        foreach ($this->options as $metadata) {
            foreach($metadata as $type => $data) {
                echo '<tr>';
                echo '<td>'.$type.'</td>';
                echo '<td>'.$data.'</td>';
                echo '</tr>';
            }
        }

    }
}

And then call it outside the class:

$object->iterate();

You can read more about this here: http://php.net/manual/en/language.oop5.iterations.php

n-dru
  • 9,285
  • 2
  • 29
  • 42