简体   繁体   English

在PHP中处理其他程序员的复杂数组

[英]dealing with other programmers' complex arrays in PHP

I am maintaining an OO PHP application that loads everything to $this array. 我正在维护一个OO PHP应用程序,它将所有内容加载到$ this数组。 When I do a var dump on $this to find out how to access a value, I get dozens of pages of output. 当我在$ this上执行var转储以了解如何访问值时,我得到了几十页的输出。 Hunting down the array elements that I need is very time consuming 寻找我需要的数组元素非常耗时

For example, if I want to find where Customer Territory is stored, I have to figure out the heirarchy of the array using print_r or var_dump and staring [ edit: and searching] ]at the output until I figure out the path. 例如,如果我想找到Customer Territory的存储位置,我必须使用print_r或var_dump并在输出处查看[ edit: and searching]]来确定数组的层次结构,直到找出路径为止。

for example: $this->Billing->Cst->Record['Territory'] 例如:$ this-> Billing-> Cst-> Record ['Territory']

Is there a better way to do this, or some tools/techniques that I can use. 有没有更好的方法来做到这一点,或者我可以使用的一些工具/技术。 For instance, is there there quick way to find the path to variable ['Territory'] throughout the array directly? 例如,有没有快速的方法直接在整个数组中找到变量['Territory']的路径?

Krumo is a graphical "var_dump" tool that may make navigation a tiny bit easier. Krumo是一个图形化的“var_dump”工具,可以使导航更容易一些。 Check out the "examples" section on the project page. 查看项目页面上的“示例”部分。

For searching in multi-dimensional arrays, this SO question may help you. 对于在多维数组中搜索, 这个SO问题可能对您有所帮助。

You could probably do ctr+F on the output instead of staring at it? 你可能在输出上做ctr + F而不是盯着它?

Just start with ctr+F: "Customer", "Territory" and all other names related to whatever you're searching. 刚开始使用ctr + F:“客户”,“区域”以及与您搜索的任何内容相关的所有其他名称。

function findInTree($var, $words) {
    $words = explode(' ', strtolower($words));
    $path = array();
    $depth = 0;
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($var), RecursiveIteratorIterator::SELF_FIRST);
    foreach ($iterator as $key => $value) {
        if ($iterator->getDepth() < $depth) {
            unset($path[$depth]);
        }
        $depth = $iterator->getDepth();
        $path[$depth] = $key;

        if (is_string($key) && in_array(strtolower($key), $words)) {
            echo '<pre>', implode(' -&gt; ', $path), '</pre>';
        }
    }
}

findInTree($this, 'Customer Territory');

This function will walk through your object and look for any of the given words as a key. 此函数将遍历您的对象并查找任何给定的单词作为键。

FirePHP/Firebug可以为您提供$this及其属性的详细结构视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM