1

I have been using PHP to get value from the database and storing them in an array and when I echo the code using implode in PHP, it is working absolutely fine(For single as well as multiple values in the data arrays.

PHP Code for Reference

$data_Sad= array();
$data_Like = array();

$data_Sad[] = 3;
//$data_Sad[] = 3;
$data_Like[] = 3;
//$data_Like[] = 3;

Now,

I want to assign that value to the javascript array. All i do is -

<script>    
js_array = new Array(<?php echo implode(',', $data_Sad);?>);
js_array1  = new Array(<?php echo implode(',', $data_Like); ?>);

alert(js_array);
alert(js_array1);
</script>

Now when I alert the js_array when it has single value then it simply returns/alerts an empty array string. While when I populate 2 or more values then it works perfectly fine.

Also, if I use json_encode then it works fine but again it is a string format output and all I need is int/number values.

Can anyone help me out solving the issue using implode of PHP or any other work around?

Thanks in anticipation :)

1
  • Have you tried using quotes? Commented Sep 25, 2017 at 4:56

3 Answers 3

1

When you construct with single integer element to array it create a array of that size, instead of creating array with that element !

Create a single value array in JavaScript

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Parameters

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

Comments

0

You should change your approach to use json_encode in php. It will ensure that the arrays are properly converted to usable JavaScript objects and can work on more complex arrays including associative array.

You can also use php short tags when simply echoing data to a template. e.g.

$data_Sad= array();
$data_Like = array();

$data_Sad[] = 3;
$data_Like[] = 3;

Now,

I want to assign that value to the javascript array. All i do is -

<script>    
js_array = <?=json_encode($data_Sad)?>;
js_array1 = <?=json_encode($data_Like)?>;

alert(JSON.stringify(js_array));
alert(JSON.stringify(js_array1));
</script>

Comments

0

Try run the below code and you will understand the problem.

<?php

$data_Sad= array();
$data_Like = array();

$data_Sad[] = 3;
$data_Sad[] = 4;
//$data_Sad[] = 3;
$data_Like[] = 3;
$data_Like[] = 5;
//$data_Like[] = 3;
echo "<pre>";print_r($data_Sad);print_r($data_Like);echo "</pre>";
?>
<script>    
js_array = new Array(<?php echo implode(',', $data_Sad);?>);
js_array1  = new Array(<?php echo implode(',', $data_Like); ?>);

alert(js_array);
alert(js_array1);
</script>

When PHP array has one numeric value, it will taking as array initialization.

To avoid such issues, use the below code.

<?php

$data_Sad= array();
$data_Like = array();

$data_Sad[] = 3;    
//$data_Sad[] = 3;
$data_Like[] = 3;    
//$data_Like[] = 3;
echo "<pre>";print_r($data_Sad);print_r($data_Like);echo "</pre>";
?>
<script>    
js_array = new Array(<?php echo '\''.implode('\',\'', $data_Sad).'\'';?>);
js_array1  = new Array(<?php echo '\''.implode('\',\'', $data_Like).'\''; ?>);

alert(js_array);
alert(js_array1);
</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.