1

I'm now implement such a task:I need to convert PHP variables to javascript

$arr['name1'] = 'value1';
$arr['name2'] = 'value2';

so that after processing,should be:

<script type="text/javascript">
var name1 = 'value1';
var name2 = 'value2';
...

I hoped to do it this way:

<script>
list(<?php echo join(',',array_keys($arr)?>) = <?php echo json_encode(array_values($arr));?>
user198729
  • 61,774
  • 108
  • 250
  • 348
  • 1
    http://stackoverflow.com/questions/1005960/searching-equivalent-function-way-to-list-from-php and http://stackoverflow.com/questions/1954426/javascript-equivalent-of-phps-list are similar. The first provides a link to and article on http://solutoire.com that provides a way to do this. There isn't a specific JS operator/syntax/function that does that, but there are many ways to accomplish this in JS. – JAL Jan 21 '10 at 17:37

6 Answers6

3

Check php.js, they have ported a lot of features of PHP to JS, list() is listed as experimental.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • List is more of a language construct; PHP JS doesn't seem to have implemented it, from what I can find. – Matchu Jan 21 '10 at 17:39
  • @Matchu: I know it's a language construct, I didn't say otherwise. I've provided a link to the php.js experimental port of `list()`. – Alix Axel Jan 21 '10 at 17:43
  • 1
    Oh, wow. Was that there before? I totally missed it. Give the post a token edit and I'll switch to upvote :) – Matchu Jan 21 '10 at 17:46
  • @Matchu: Just did, if you upvote me I won't win any rep points as I'm already capped for today - but it can reflect the possible usefulness of the answer. =) – Alix Axel Jan 21 '10 at 18:01
  • @AlixAxel, second link in the post is dead. – Pang Jan 25 '17 at 09:17
3

Probably misunderstood the question, but wouldn't this work?

echo "<script>";
foreach ($arr as $k => $v) 
{
    echo "var $k = $v;";
}
echo "</script>";
Per H
  • 1,542
  • 10
  • 19
  • Yes,that's almost what I mean.But I'm just not sure if there is a more concise solution like `list`? – user198729 Jan 21 '10 at 17:42
  • Not built into Javascript. You'd have to use a custom function for that, which would be pretty silly when you can just use an extremely simple loop - not to mention that the loop would be far more readable to other developers. – Matchu Jan 21 '10 at 17:50
  • If I'm not counting wrongly,you've mentioned silly 2times.You can go away!I don't need your help at all! – user198729 Jan 21 '10 at 17:53
  • @unknown But it is silly. Just like with your last question, you are creating problems where there are none. Just write the variables in JSON or use `foreach`. What is so much more concise about `list`? – Gordon Jan 21 '10 at 18:01
1

Nope, sorry.

You might instead consider simply using json_encode to print the object to the script.

<?php
    $a = array('foo' => 'bar');
    json_encode($a); // returns {"foo":"bar"} which is valid Javascript syntax
?>

<script>var obj = <?php echo json_encode($a); ?></script>
Matchu
  • 83,922
  • 18
  • 153
  • 160
0

You could use json.

var arr = { "name1" : "value1", "name2": "value2"}

Then to get a value you just access it like a field

var firstVal = arr.name1
TwentyMiles
  • 4,063
  • 3
  • 30
  • 37
0

you might want to have a look at PHP JS, it will help you in your conversion tasks. Thanks

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • List is more of a language construct; PHP JS doesn't seem to have implemented it, from what I can find. – Matchu Jan 21 '10 at 17:40
0

Currently it's only possible by help of the controversial 'eval()' function...

function list(v,a){
  v=v.split(',');
  for(var s='',i=0;i<v.length;i++)
    s+=v[i]+'='+i+'<'+a+'.length?'+a+'['+i+']:null;';
  return s;
}

Usage:

eval(list(var_names_as_comma_separated_str, str_name_of_array));

Example:

A=['one',1,'z1'];
eval(list('x,y,z','A'));
console.log('+-> First: ',x,y,z);  //output: one 1 z1
(function(){
  var z,B=['two',2,'local'];  //note that z is local variable
  console.log('Local.Before: ',x,y,z);  //output: one 1 undefined
  eval(list('x,y,z','B'));
  console.log('Local.After: ',x,y,z);  //output: two 2 local
})()
console.log('+-> Back: ',x,y,z);  //output: two 2 z1 (global z is unchanged)

Note: window['var_name'] can't be used to access local variable (variable inside function), so we have no choice other than eval()...

Ferdinand Liu
  • 314
  • 1
  • 3