简体   繁体   English

PHP如果没有索引,如何访问多维数组的所有元素?

[英]PHP How to access all elements of multidimensional array if no indexes are known?

I used a modified version of the code here Determining what classes are defined in a PHP class file to create a multidimensional array of classes and functions from a specific list of files. 我在这里使用了代码的修改版本确定在PHP类文件中定义了哪些类,以从特定的文件列表创建类和函数的多维数组。 What I ended up with is this (I know the names are messed up but I was goofing off and would have to go back and change class names and functions names in 3 different files so lets assume these were legitimate class names and function names), 我最终得到的是这个(我知道这些名字搞砸了,但是我已经搞砸了,并且必须返回并更改3个不同文件中的类名和函数名,所以我们假设这些是合法的类名和函数名),

Array
(
    [0] => Array
        (
            [namethis_whatever_I_want] => Array
                (
                    [0] => another_function
                    [1] => whatever_function
                )

        )

    [1] => Array
        (
            [tc_class_simplevar] => Array
                (
                    [0] => set_var_test
                    [1] => get_var_test
                )

        )

    [2] => Array
        (
            [another_freekin_class] => Array
                (
                    [0] => some_function
                    [1] => another_crazy_function
                )

        )

)

So now I need to be able to access the class names and the function names under each class without knowing what the index is for any of them. 所以现在我需要能够访问每个类下的类名和函数名,而不知道它们中的任何索引是什么。 I've tried for loops, foreach, and using counters like $i and $ii to access them by there numerical index but nothing I try will print out anything but garbage or errors. 我已经尝试过循环,foreach,并使用像$ i和$ ii这样的计数器来访问它们的数字索引,但我尝试的任何东西都会打印出除垃圾或错误之外的任何内容。

I was thinking something like embedded foreach statements 我在想类似嵌入式foreach语句的东西

$i = 0; 
foreach($array as $class){
  echo $class[$i];
  $ii = 0; 
  foreach($class as $val){
  echo $val[$ii];
  $ii++;
  }
$i++;
}

But no luck with that. 但没有运气。

Also trying to access $array[$i][$i]; 还试图访问$ array [$ i] [$ i]; or $array[$i][$ii]; 或$ array [$ i] [$ ii]; throws an error bad offset 0 抛出错误坏偏移0

I'm sure there will be an issue with the class array index actually being named the class name but I was thinking I could still use the numerical index for that. 我确定类数组索引实际上被命名为类名会有问题,但我想我仍然可以使用数字索引。

Basically I am totally confused about how to access the data and could use a point in the right direction. 基本上我对如何访问数据完全感到困惑,并且可以在正确的方向上使用一个点。

I will need to be able to get a class name and then get all of the function names that are under that class and will need to be able to access them at different points throughout my program by retrieving them from the array. 我需要能够获得一个类名,然后获取该类下的所有函数名,并且需要能够通过从数组中检索它们来在整个程序中的不同点访问它们。

Thank you 谢谢

I hate answering my own question just minutes after asking others for help. 在讨厌别人寻求帮助后几分钟我讨厌回答我自己的问题。 I feel as though I wasted your time. 我觉得好像浪费了你的时间。

You guys are right about the foreach but this was a tricky one. 你们对foreach是正确的,但这是一个棘手的问题。 Asking the question was kinda helpful in talking myself through what I needed to find so it dawned on me where the problem was. 问这个问题对于通过我需要找到的内容进行自我介绍是有帮助的,所以问题就在于我。

There are 3 layers to this array. 这个数组有3层。 There's an array containing 3 arrays and each of them has a string instead of numerical for it's index. 有一个包含3个数组的数组,每个数组都有一个字符串而不是数字的索引。 Each of them containing there own elements. 每个都包含自己的元素。

So I had to iterate through arrays 1,2,3 getting the string index's of each of their elements and then use that string element along with numerical elements to get the inner elements of the inner most arrays. 所以我不得不遍历数组1,2,3获取每个元素的字符串索引,然后使用该字符串元素和数字元素来获取最内部数组的内部元素。 Ok that confused me but here's the code that worked for me, using echo's and some slight formatting so I could see it working. 好吧,这让我感到困惑,但这里的代码对我有用,使用echo和一些轻微的格式,所以我可以看到它正常工作。

$size = sizeof($objectList);
for($i = 0; $i < $size; $i++){
    foreach($objectList[$i] as $key => $val){
        $className = $key;
        echo $className . ": <br/>";
        foreach($objectList[$i][$className] as $val){
            $functionName = $val;
            echo $functionName . " , ";
        }
    echo "<br/><br/>";
    }
}

Resulted in 导致

namethis_whatever_I_want: another_function , whatever_function , namethis_whatever_I_want:another_function,whatever_function,

tc_class_simplevar: set_var_test , get_var_test , tc_class_simplevar:set_var_test,get_var_test,

another_freekin_class: some_function , another_crazy_function , another_freekin_class:some_function,another_crazy_function,

For anyone else in case it helps, here's Marks version with my arrays and variable names to compare. 对于其他任何人,如果它有帮助,这里是标记版本与我的数组和变量名称进行比较。 I'd say this way is the cleanest and it cuts out a couple lines of code. 我会说这种方式最干净,它会删除几行代码。

foreach($objectList as $objects){
    foreach($objects as $classname => $functions){
        $cn = $classname;
        foreach($functions as $functionname){
            $fn = $functionname;
        }
    }
}

Thank you for your help :-) 谢谢您的帮助 :-)

foreach also allows you to specify a key portion of the iterator, so you can loop through associative arrays as: foreach还允许您指定迭代器的关键部分,因此您可以循环关联数组:

foreach($array as $key => $val){
    echo $array[$key]; // prints $val
}

So, in your case: 所以,在你的情况下:

foreach($rootarray as $classarray){
    foreach($classarray as $classname => $functions){
        // $classname contains the name of the class
        foreach($functions as $functionname){
            // $functionname contains the name of the function
        }
    }
}

foreach是你的朋友:

foreach($array as $key => $value) echo('$array['.$key.'] = '.$value);

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

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