0

I have a php array of key => value, and I would like to store all the values into a list (like a python list) of values. I need it cause I have to pass this list of values to a JQuery script.

Basically I have:

$var = Array ( [0] => 5 [1] => 7 [2] => 9 [3] => 10 )

and I would like to have something like

$var = [5, 7, 9, 10];

When I call from Jquery my variable:

var newvariable = "<?php echo $var ?>";

The final goal is to have a Jquery list like:

var newvariable = [5,7,9,10];

Is this possible in php?

5
  • 1
    Check out json_encode(): php.net/manual/en/function.json-encode.php Commented Feb 20, 2017 at 12:16
  • There's no difference between those two arrays. An array whose keys are sequential integers starting from 0 is the same as an ordinary indexed array. Commented Feb 20, 2017 at 12:19
  • Do you mean a string like $var = "[5, 7, 9, 10]"; Commented Feb 20, 2017 at 12:20
  • @Barman no, basically my jQuery var should be: var values = [5, 7, 9, 10]; Commented Feb 20, 2017 at 12:23
  • $var = implode(',',$var); try this Commented Feb 20, 2017 at 12:25

4 Answers 4

1
$var = array(5,4,7,5,6,8);
echo json_encode($var);
Sign up to request clarification or add additional context in comments.

Comments

0

Check this PHP code :

<?php
  $var = array(5, 7, 9, 10);
  $data = json_encode($var) ;
  echo $data ; //[5,7,9,10]
?>

Comments

-1

I'm assuming you have a PHP array such as this:

$var = array ( 5, 7, 9, 10 );

This would print, with var_dump:

array(4) {
  [0]=>
  int(5)
  [1]=>
  int(7)
  [2]=>
  int(9)
  [3]=>
  int(10)
}

PHP has a list() language constructor [1]. I don't believe this is what you want though.

Since I'm not sure exactly how this is constructed, I'd suggest looking at the JSON functions [2] PHP has available which might be of help when interacting with jQuery.

Not a definitive answer but I hope it helps.

[1] http://php.net/manual/en/function.list.php
[2] http://php.net/manual/en/ref.json.php

Comments

-3

Not sure what is going on here.

$var = Array ( [0] => 5 [1] => 7 [2] => 9 [3] => 10 )

Is not valid PHP or valid JS. It would need to look something like:

$var = [0 => 5, 1 => 7, 2 => 9, 3 => 10];

It creates an Array with given keys.

array_keys($var) Will extract the keys.
array_values($var) Will extract the values.

To get a string that looks like

var newvariable = [5,7,9,10];

you can use

var newvariable = "<?php echo json_encode($var) ?>";

But that's just because json_encode creates a string from any variable be it a string, object or boolean.

More strict would be something like:

var newvariable = "[<?php echo join(',', array_values($var)); ?>]";

1 Comment

Array ( [0] => 5 [1] => 7 [2] => 9 [3] => 10 ) is just a print_r output. and you may learn how does json_encode work on the respective manual page

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.