如何在没有索引的情况下访问多维数组的所有元素? [英] PHP How to access all elements of multidimensional array if no indexes are known?

查看:119
本文介绍了如何在没有索引的情况下访问多维数组的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用了一个修改版本的代码确定哪些类是在PHP类文件中定义的,以便从特定的文件列表中创建类和函数的多维数组。我最终得到的是这样的(我知道这些名字已经搞乱了,但是我不知所措,不得不回头改变3个不同文件中的类名和函数名,所以我们假设这些是合法的类名和函数名)数组

[0] =>数组



  [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] =>数组

[another_freekin_class] =>数组

[0] => some_function
[1] => another_crazy_function







所以现在我需要能够访问每个类下的类名和函数名,而不必知道它们中的索引是什么。我已经尝试了循环,foreach,并使用像$我和$ ii这样的计数器通过数字索引来访问它们,但没有任何我会尝试打印出来的东西,但垃圾或错误。

  $ 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];抛出一个错误错误的偏移量0



我相信会有类数组索引实际被命名类名称的问题,但我想我仍然可以使用数值指数。

基本上,我对如何访问数据感到十分困惑,并可能使用正确的方向。

我需要能够获得一个类名,然后获取该类下的所有函数名,并且需要能够在整个程序的不同点访问它们,方法是从数组。

谢谢

我讨厌在问别人求助后几分钟回答我自己的问题。我觉得好像浪费了你的时间。

你们是对的,但这是一个棘手的问题。问这个问题有助于我自己通过我需要找到的东西来自我介绍,所以问题出现在我身上。



这个数组有三层。有一个包含3个数组的数组,每个数组都有一个字符串,而不是索引的数字。他们每个人都包含自己的元素。

所以我不得不遍历数组1,2,3得到每个元素的字符串索引,然后使用该字符串元素和数字元素来获得内部最内层阵列的元素。好吧,让我感到困惑,但是这里的代码适合我,使用echo和一些轻微的格式化,所以我可以看到它的工作。

  $ size = sizeof($ objectList); ($ 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 />;


$ / code $ / pre

导致

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,

对于其他人来说,如果它有帮助,这里是Marks版本与我的数组和变量名称进行比较。

  foreach($ objectList as $ object)这个方法是最简单的,它会删除几行代码。 ){
foreach($ objects为$ classname => $ functions){
$ cn = $ classname;
foreach($函数为$函数名){
$ fn = $函数名;





感谢您的帮助: - )

解决方案

foreach 也允许你指定迭代器的关键部分,所以你可以通过关联数组来循环:

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

所以,在你的情况下:

  foreach($ rootarray as $ classarray){
foreach($ classarray as $ classname => $ functions){
// $ classname包含类的名称
foreach($函数为$函数名){
// $函数名包含函数的名称
}
}
}


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 :-)

解决方案

foreach also allows you to specify a key portion of the iterator, so you can loop through associative arrays as:

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
        }
    }
}

这篇关于如何在没有索引的情况下访问多维数组的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆