2

I'm trying to adjust a php chart library on my code with some data fetched from a database. I am too close but i think i am confused with all these arrays. I will try to explain as easy as possible. The appropriate form of constructing the data that my library needs to read in order to create a php chart is the following:

//With this value my api displays the chart normally
$p->data = array(array(array("2015/10/10",44),array("2015/10/11",56)));

The way i'm trying to construct the aforementioned array is the following

//Fetch results from database and push values into an array
while($row = mysqli_fetch_array($result)){ 
  $values[] = array($row['Date'] => $row['Total']);
}
$p->data = array(array($values));

But unfortunately the chart is not displaying my values. How can i fix my code in order to achive this format:

$p->data = array(array(array("2015/10/10",44),array("2015/10/11",56)));

3 Answers 3

1

You are inserting $row['Date'] as the key and $row['Total'] as the value, which, if you run print_r($p->data) results in this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [2015/10/10] => 44
                        )
                    [1] => Array
                        (
                            [2015/10/11] => 56
                        )
                )
        )
)

You actually need

while($row = mysqli_fetch_array($result)){ 
  $values[] = array($row['Date'], $row['Total']);
}

This way, you will get this array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => 2015/10/10
                            [1] => 44
                        )
                    [1] => Array
                        (
                            [0] => 2015/10/11
                            [1] => 56
                        )
                )
        )
)

Exactly what you need

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

Comments

1

If the array should be the same as the first code example, you don't need the =>. So, you can just do the following:

while($row = mysqli_fetch_array($result)){ 
  $values[] = array($row['Date'], $row['Total']);
}

The => creates a key value pair with the first item as the key and the second as the value. If you use a ,, it creates a flat array with those items as values.

1 Comment

Thank you very much all!
0

You're wrapping one too many arrays:

$p->data = array(array(array("2015/10/10",44),array("2015/10/11",56)));
            ^-A   ^-B   ^-C0                    ^-C1

and then

  $values[] = array($row['Date'] => $row['Total']);
               ^-C0,C1,C2,etc...
      ^--B
}
$p->data = array(array($values));
                   ^--A
              ^---????

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.