1

Admittedly, my understanding of arrays is junior at best (although before this I thought I had them figured out).

Essentially, I am trying to convert a PHP array to a JS array. The purpose for this is that ultimately I'd like to create a graph/chart using jquery (I've tried using php charts, but they haven't worked for me to this point).

Long story short, every time I try and convert my PHP array to a JS array, the output is "null". This may be due to my understanding of arrays...

//this query gets me the count of each type of variabletype

$sql = "SELECT variabletype, COUNT(variabletype) AS value_occurrence FROM variable GROUP BY variabletype ORDER BY value_occurrence DESC";
$vars_query = mysqli_query($con,$sql) or die(mysqli_error($con));

 while($vars = mysqli_fetch_array($vars_query, MYSQLI_ASSOC)) 
    {

//this is a sub-query that gets me how many of those records are considered "positive"
$times_positive_qry = mysqli_query($con, "SELECT variable.variabletype, COUNT(value.valueid) AS positive_occurrence FROM variable INNER JOIN value On variable.variableid=value.variableid WHERE variable.variabletype = '" .$vars['variabletype']. "' AND value.valuelift>0.00 AND value.valuesignificant=1 GROUP BY variable.variabletype ORDER BY positive_occurrence DESC");
$times_positive = mysqli_fetch_array($times_positive_qry, MYSQLI_ASSOC);
$pos = $times_positive['positive_occurrence'];

}

//this is the code that is supposed to take my php array and turn it into JS array.
 echo "<script type='text/javascript'>";
 $php_array = $pos;
 $js_array = json_encode($php_array);
 echo "var javascript_array = ". $js_array . ";\n";
 echo "</script>";

Again, my understanding of arrays are junior, but I think it may have to do with my use of $pos. Is this not an array? or am I missing the mark somewhere else?

Any help would be much appreciated!

3
  • 1
    try a var_dump on $pos and see what the type is. That'll answer your question why it's returning null from json_encode Commented Mar 17, 2014 at 15:15
  • On a sidenote, you might want to read up on arrays in the official PHP manual, so your understanding of them increases :) Commented Mar 17, 2014 at 15:16
  • 1
    your while loop assigns a scalar value to $pos, so it's definitely not an array. and i'd bet the code is not executed at all (mysqli_fetch_array($vars_query, ...) returns no rows), so $pos is not defined, and you've shot yourself in the foot with insufficient error_reporting. Commented Mar 17, 2014 at 15:17

3 Answers 3

3

You could explicitly declare $pos as an array like this:

$pos = array();

Inside the while loop, you can ensure that it's an array and that you don't lose data while iterating over the loop by using brackets like so:

$pos[] = $times_positive['positive_occurrence'];

That syntax automatically creates an indexed array. If you don't use that syntax, then the value of $pos will be only the value assigned on the final loop of the while statement.

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

3 Comments

I get the following error: "Fatal error: [] operator not supported for strings in /blah/blah/blah/filename.php. I added $pos[] = $times_positive['positive_occurrence']; in my while loop. Any thoughts here?
That probably means the $pos is declared with a value somewhere earlier in the code, so it's already a string variable. Make sure to add the explicit declaration $pos = array(); just before the while loop.
Great! Did you mean to accept the other answer though?
1

You have this line:

$pos = $times_positive['positive_occurrence'];

It assignes a value to the variable $pos. The value you are assigning is probably not an array, but besides, you overwrite $pos every time in the loop.

So I think you meant to do this:

$pos[] = $times_positive['positive_occurrence'];

That will treat $pos as an array and appends every new value to the end of the array.

You may want to start your script by writing $pos = array();. That way, you make sure $pos already exists and is a valid (empty) array in case the query doesn't return any result.

2 Comments

I get the following error: "Fatal error: [] operator not supported for strings in /blah/blah/blah/filename.php. I added $pos[] = $times_positive['positive_occurrence']; in my while loop. Any thoughts here?
That means $pos was already a string before entering the loop. Maybe you've accidentally recycled the same variable name? If you add $pos = array(); before the loop, you should be fine, although for readability it's better to use a more descriptive variable name, and not to use the same name for two different purposes.
0

this might be the solution you want.

  1. before while loop just declare $pos = array();
  2. and in the while loop use $pos as $pos[]

and your javascript looks like this:

<script type='text/javascript'>
<?php
//this is just and example you can replace it with $pos[]
$php_array = array('abc','def','ghi');
?>
var js_array =<?php echo json_encode($php_array);?>;//do not add quotes to this line
alert(js_array[0]);
</script>

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.