0

I have the following json encoded array which I pass to JQuery as follows, and everything works fine when when there are elements in the array. However, there are times when the array might be empty depending upon a users selections, and that is where my script breaks down:

PHP
$myleaderarray =  [{"Week_count":"3","Userid":"102","Username":"gsd555","MedCondPrimary":"Diabetes"},{"Week_count":"0","Userid":"216","Username":"gsd3","MedCondPrimary":"Cardiac Respiratory"}]
$count = count($myleaderarray);   //1

JQ
var myleaderarray = <?php echo $myleaderarray; ?>; // this passes the       array as expected
console.log(myleaderarray.length); //2

This is what I see in the console when the json_encoded array is empty

JQ
var myleaderarray = ;
"Unexpected token ';'"

I can't seem to test for this error in JQ so must I do this on the server-side, or am I completely missing something? Thanks for the help.

1
  • 1
    could you please post proper code? also, i dont see any json_encode. Your problem is that your string is empty, but it should be []. if you pass a proper array to json_encode is should always work. but i cant really tell you what you have to do because the code you posted here is messed up Commented Feb 27, 2014 at 21:41

2 Answers 2

0

change:

var myleaderarray = `<?php echo $myleaderarray; ?>;` 

to

var myleaderarray = "<?php echo $myleaderarray ?>"; 
5
  • quotes aren't really useful here. when it's a string you would have to parse it, but javascript can simply use json values. also, you would need to escape it. Commented Feb 27, 2014 at 21:44
  • I tried the double quotes before posting this question and when I do I receive the following SyntaxError: "Expected an identifier but found 'Week_count' instead". Week_count is the first key in the array Commented Feb 27, 2014 at 21:48
  • @user2232681 post proper code and you get a proper answer. the first php line is invalid php syntax (fatal error). the first javascript line is invalid too, also a syntax error, so how do you expect anyone to help you? (` are illegal characters in javascript outside strings) Commented Feb 27, 2014 at 21:51
  • the single quotes are not in my code, my mistake here. Commented Feb 27, 2014 at 21:57
  • @user2232681 well, you said something about json_encode in your question, but i don't see any in your code, i also assume that you dont use it, because json_encode always returns javascript compatible code or it throws an error/warning Commented Feb 27, 2014 at 22:02
0
$myleaderarray =  ['{"Week_count":"3","Userid":"102","Username":"gsd555","MedCondPrimary":"Diabetes"}','{"Week_count":"0","Userid":"216","Username":"gsd3","MedCondPrimary":"Cardiac Respiratory"}'];
$count = count($myleaderarray);   // is now two

JQ
var myleaderarray = <?php var_dump($myleaderarray); ?>; // this passes the       array as expected
console.log(myleaderarray.length);

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.