I have this code (below) working just fine, however whenever I try to ‘wrap’ it in a function eg. function systemLoop($series) { ... } I get nothing but uber fail.
I’m new to PHP (and coding at this level) and multidimensional arrays. So getting it to work as a function is even more alien to me. However since I have a “few” system series arrays to go through I thought to have it in a function could be a better solution. So all I have to do is call systemLoop("hyper"); systemLoop("business");
Please can you help? Am I doing the right thing by trying to turn it into a function? If am on the right step is there a possible step i’m excluding to making the function work as intended?
$series = "super";
foreach ( $systems[$series] as $names => $name ) {
echo "<ul>";
if (is_array($name)){
echo "<h2>$names => $name</h2>";
foreach( $systems[$series][$names] as $details => $detail ){
if (is_array($detail)){}else{
echo "<h3>$details => $detail</h3>";
}
}
foreach( $systems[$series][$names]['components'] as $components => $component ){
if(is_array($component)){
foreach( $systems[$series][$names]['components']['cpu'] as $part => $specs ){
echo "<li>cpu $part => $specs</li>";
}//ends foreach
}else{
echo "<li>$components => $component</li>";
}//ends if is_array
}// ends foreach
}else{
echo "<li>$name</li>";
}//ends if is_array
echo "</ul>";
echo "<hr/>";
}//ends foreach
It sounds like your problem with wrapping it in a function comes from not passing the $systems variable, which you can do either as a global or a parameter in the function call. I’ll show the 2 examples here:
Passing $systems as a parameter:
Which you would call like this:
Or passing $systems as a global:
Then you would call the function like this:
From what it sounds like the problem might be, either way should fix the problem you’re having.