简体   繁体   English

将php数组值作为JavaScript数组值传递

[英]Pass php array values as JavaScript array values

I have a PHP array like this. 我有一个这样的PHP数组。

Array
(   
  [0] => hal([[br,1],[cl,4]])
  [1] => alk([[nc(1),1,2],[nc(1),3]])
)

How to pass to JavaScript like below. 如下所示如何传递给JavaScript。

var hal=[["br",1],[cl,4]];
var alk=[[nc(1),1,2],[nc(1),3]];

I write some code 我写一些代码

<script>
  var data = <?=json_encode($input);?>;    //$input is name of the php array
  var hal=[data[0].substring(5,data[0].lastIndexOf(']'))];
  var alk=[data[1].substring(5,data[1].lastIndexOf(']'))];
  document.write(hal[0]);
</script>

The output is [br,1],[cl,1] and my expected output is like the one below.Any ideas? 输出为[br,1],[cl,1],我的预期输出如下所示。有什么想法吗? Thank you. 谢谢。

document.write(hal[0]);  => ["br",1]
document.write(hal[0][0]); => ["br"]

If you want multiple variables, you'll want to loop through the array; 如果您想要多个变量,则需要遍历数组。 you can grab the names using a regular expression . 您可以使用正则表达式获取名称。 If you're trying to turn this into valid data you can parse, like a JSON string, you're going to have to do an awful lot of work; 如果您试图将其转换为可以解析的有效数据(例如JSON字符串),则将需要进行大量工作; likely wherever you're getting this string from would be a better place to look to. 无论您从何处获取此字符串,都可能是一个更好的地方。 Have them pass you a valid JSON string instead. 让它们为您传递有效的JSON字符串。

<script>
<?php foreach($input as $v) {
    preg_match("/(\w+)\((.*)\)/", $v, $matches);
    $var = $matches[1];
    $val = str_replace("'", "\\'", $matches[2]);
    echo "var $var = '$val';\n";
} ?>
</script>

There's a package called Transform PHP Vars to JavaScript by Jeffrey Way that you can use to transfer your variable easily to your Javascript. Jeffrey Way提供了一个名为“ 将PHP Vars转换为JavaScript”的程序包,您可以使用它轻松地将变量传输到Javascript。

First, create an implementation of the Laracasts\\Utilities\\JavaScript\\ViewBinder interface. 首先,创建Laracasts\\Utilities\\JavaScript\\ViewBinder接口的实现。 This class is in charge of inserting the given JavaScript into your view/page. 此类负责将给定的JavaScript插入视图/页面。

<?php

class MyAppViewBinder implements Laracasts\Utilities\JavaScript\ViewBinder {

    // $js will contain your JS-formatted variable initializations
    public function bind($js)
    {
        // Do what you need to do to add this JavaScript to
        // the appropriate place in your app.
    }
}

Next, put it all together: 接下来,将它们放在一起:

$binder = new MyAppViewBinder;
$javascript = new PHPToJavaScriptTransformer($binder, 'window'); // change window to your desired namespace

$javascript->put(['foo' => 'bar']);

Now, you can access window.foo from your JavaScript. 现在,您可以从JavaScript访问window.foo

I found an answer for my problem.Here i pass value from php as string.Then that string convert it to an object. 我找到了问题的答案。在这里,我将php中的值作为字符串传递,然后将该字符串将其转换为对象。
<?php
$input="hal([[br,1],[cl,4]])";
preg_match('@^([^[]+)?([^)]+)@i',$input, $hal); //get string as '[[br,1],[cl,4]]'
?>

<script>
var hal1 = <?=json_encode($hal[2]);?>; //'[[br,1,2],[cl,1,2]]' pass to javaScript

function toJSObject(str){
str = str.replace(/([a-zA-Z]+)/g, '"$1"');
return (JSON.parse(str))
}
var hal = toJSObject(hal1); // pass to JS object [['br',1,2],['cl',1,2]]
document.write(hal);
</script>
And also get this as right. 并得到正确的答案。
document.write(hal[0]); => ["br",1]
document.write(hal[0][0]); => ["br"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM