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
}
}
}
foreach
is your friend:
foreach($array as $key => $value) echo('$array['.$key.'] = '.$value);
精彩评论