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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题