PHP 如果没有已知索引,如何访问多维数组的所有元素?
我在这里使用了代码的修改版本 确定 PHP 类文件中定义了哪些类,以从特定文件列表创建类和函数的多维数组。我最终得到的是这样的(我知道名称搞砸了,但我在偷懒,必须返回并更改 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
)
)
)
所以现在我需要能够访问每个类下的类名和函数名,而不知道它们的索引是什么。我尝试过 for 循环、foreach 并使用 $i 和 $ii 等计数器通过数字索引访问它们,但我尝试的任何操作都不会打印出除了垃圾或错误之外的任何内容。
我在想类似嵌入 foreach 语句的东西
$i = 0;
foreach($array as $class){
echo $class[$i];
$ii = 0;
foreach($class as $val){
echo $val[$ii];
$ii++;
}
$i++;
}
,但没有运气。
还尝试访问 $array[$i][$i];或 $array[$i][$ii];抛出错误 bad offset 0
我确信类数组索引实际上被命名为类名会有问题,但我想我仍然可以使用数字索引。
基本上我对如何访问数据完全感到困惑,并且可以在正确的方向上使用一个点。
我需要能够获取一个类名,然后获取该类下的所有函数名称,并且需要能够通过从数组中检索它们来在整个程序的不同点访问它们。
谢谢,
我讨厌在向别人寻求帮助后几分钟就回答自己的问题。我觉得我浪费了你的时间。
你们对 foreach 的看法是正确的,但这是一个棘手的问题。提出这个问题对我自己了解我需要找到的内容很有帮助,所以我明白了问题出在哪里。
该阵列有 3 层。有一个包含 3 个数组的数组,每个数组都有一个字符串而不是数字作为索引。它们每个都包含自己的元素。
因此,我必须迭代数组 1,2,3 获取每个元素的字符串索引,然后使用该字符串元素和数字元素来获取最内部数组的内部元素。好吧,这让我很困惑,但这是对我有用的代码,使用了 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/>";
}
}
结果是
namethis_whatever_I_want: another_function ,whatever_function ,
tc_class_simplevar: set_var_test 、 get_var_test 、
another_freekin_class: some_function , another_crazy_function ,
对于其他人来说,如果有帮助的话,这里是标记版本,其中包含我的数组和变量名称以进行比较。我想说这种方式是最干净的,它减少了几行代码。
foreach($objectList as $objects){
foreach($objects as $classname => $functions){
$cn = $classname;
foreach($functions as $functionname){
$fn = $functionname;
}
}
}
感谢您的帮助 :-)
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. 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),
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.
I was thinking something like embedded foreach statements
$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]; or $array[$i][$ii]; throws an error bad offset 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. 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. There's an array containing 3 arrays and each of them has a string instead of numerical for it's index. 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. 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.
$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 ,
tc_class_simplevar:
set_var_test , get_var_test ,
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 :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
foreach
还允许您指定迭代器的关键部分,以便您可以循环遍历关联数组为:所以,在你的情况下:
foreach
also allows you to specify a key portion of the iterator, so you can loop through associative arrays as:So, in your case:
foreach
是你的朋友:foreach
is your friend: