0

I have loaded SQL data into a bunch of arrays. I would like to use those array to create one big multidimensional array.

for ($h=0; $h<=count($siteName); $h++){

    for ($x=0; $x<=count($pluginNames); $x++)
    {
        for ($y=0; $y<=count($wpvData); $y++)
        {
            $plugData = array($siteName[$h]=>array($pluginNames=>array($pluginNames[$x],$vData[$y],$wpvData[$y],$row[$y])));
        } 
    } 
}

The error I get is:

Notice: Undefined offset: 0 in /Applications/MAMP/htdocs/BJANSDJNAS.php on line 124

Warning: Illegal offset type in /Applications/MAMP/htdocs/BJANSDJNAS.php on line 124

Line 124 = $plugData array line.

Is this even possible?

Is there a better way to do this all together?

My goal is to store a bunch of data related to a website in a multidimensional array so I can display it in html divs. Basically I have a wordpress site and I would like to manage the plugins with a nice GUI via html.

3
  • What do you mean by array($pluginNames=>array(...))? $pluginNames is an array, how can it be the key in an associative array? Commented Oct 1, 2013 at 16:42
  • The first error message means that one of the arrays doesn't have an element number 0. Are your original arrays indexed or associative? Maybe you should use foreach instead of for. Commented Oct 1, 2013 at 16:44
  • pluginNames holds all the names for the plugins.... I want the name of each plugin to have a its own set of data. Wouldn't I use the name at index whatever for the key? What I am doing may be very wrong Thats why came here for help Commented Oct 1, 2013 at 16:45

1 Answer 1

1

What you want is probably something like this:

$plugData = array();
foreach ($siteName as $site) {
    $plugData[$site] = array();
    foreach ($pluginNames as $plugin)
        $plugData[$site][$plugin] = array();
        foreach ($wpvData as $y => $wpv) {
            $plugData[$site][$plugin][] = array(
                'plugin' => $plugin,
                'vdata' => $vdata[$y],
                'wpv' => $wpv,
                'row' => $row[$y]);
        }
    }
}

Notice that the syntax for adding an element to an array is $arrayname[] =; $arrayname = simply replaces the whole array, it's not something you usually want in a loop.

Sign up to request clarification or add additional context in comments.

11 Comments

I tried this and it resulted in : Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/BJANSDJNAS.php on line 121 var_dump ==== array(0) { }
It was the start of the foreach. I switched it to a different variable and it worked kind of.
There are 3 foreaches, I wanted to know which one. Anyway, the error means that the variable in the foreach isn't actually an array.
The variable $siteName is singular -- is it one site name or an array of site names? Maybe there's another variable $siteNames that you should be looping over? Without seeing more of your program, I have to make assumptions based on the original code, even though it's broken.
It was the FIRST foreach loop. And I changed siteName to tableName which is another variable I am using to hold the same data, siteName is created within a while loop it might get destroyed after the execution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.