0

I'm trying to build a multidimensional associative array but the first array is empty. As you can see the [domain1] array is not showing any associative array items whereas [domain2] is showing the results that should be in the [domain1] array.

foreach ($db->query($sql) as $row) {
            $arr1['domain'.$x] = $arr2;
            $arr2['sum'] = $row['domain' . $x];
            $arr2['core'] = ${"d$x"};
        }

My results look like this.

Array
(
[domain1] => Array
    (
    )

[domain2] => Array
    (
        [sum] => 8
        [core] => 4
    )

[domain3] => Array
    (
        [sum] => 8
        [core] => 3
    )

[domain4] => Array
    (
        [sum] => 8
        [core] => 2
    )

[domain5] => Array
    (
        [sum] => 8
        [core] => 3
    )

[domain6] => Array
    (
        [sum] => 8
        [core] => 6
    )
)
2
  • So, what is your question? What is the problem? Commented Jan 23, 2016 at 23:27
  • Hi David, as stated: "the [domain1] array is not showing any associative array items whereas [domain2] is showing the results that should be in the [domain1] array." Question/problem answered by JesusTheHun. Thanks anyway. Commented Jan 25, 2016 at 14:55

1 Answer 1

2

You set the array $arr1 in the wrong order. Correct order is :

foreach ($db->query($sql) as $row) {
    $arr2 = array();
    $arr2['sum'] = $row['domain' . $x];
    $arr2['core'] = ${"d$x"};
    $arr1['domain'.$x] = $arr2;
}
Sign up to request clarification or add additional context in comments.

Comments

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.