doujiu8918 2015-03-09 20:15
浏览 66

如何在表中打印复杂的多维关联数组(PHP)

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
        (
        )
)
  • 写回答

2条回答 默认 最新

  • douwudie8060 2015-03-09 20:46
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog