在JavaScript中将String转换为Array

时间:2021-11-04 21:45:06

I have a PHP String like this:

我有一个像这样的PHP字符串:

$string = 'a => 1 , b => 2 , c => 3 , d => 4';

which I have built from data from a database, where each "x => y" represents an associative relationship between x and y.

我从数据库中的数据构建,其中每个“x => y”表示x和y之间的关联关系。

I am then transferring this string to the html document (JQuery) and attempting to make an array out of it in JavaScript, so that I may process on the client side.

然后我将此字符串传输到html文档(JQuery)并尝试在JavaScript中创建一个数组,以便我可以在客户端进行处理。

QUESTION:

题:

Is it possible to create a JavaScript Array out of a string?
For example I need to be able to retrieve the values in the array by calling 'alert(array["c"]);' or something similar.

是否可以从字符串中创建JavaScript数组?例如,我需要能够通过调用'alert(array [“c”]);来检索数组中的值。或类似的东西。

NOTE - feel free to alter the semantics of the above string ($string) as necessary.

注 - 根据需要随意更改上述字符串($ string)的语义。

Any help appreciated guys....

任何帮助赞赏的人......

4 个解决方案

#1


3  

PHP Code

(for instance, a file, like array.php):

(例如,一个文件,如array.php):

$array=array(
    'a'=>'I am an A',
    'param2_customname'=>$_POST['param2'],
    'param1'=>$_POST['param1']
);
echo json_encode($array);

jQuery code:

$.post('array.php', {'param1': 'lol', 'param2': 'helloworld'}, function(d){
   alert(d.a);
   alert(d.param2_customname);
   alert(d.param1);
},'json');

You can use this for arrays, remember the 'json' tag at the end of the request everytime.

你可以将它用于数组,每次都记住请求末尾的'json'标记。

#2


6  

You should look into JSON. PHP has functions to write JSON and javascript can decode such JSON objects and create native objects

你应该研究一下JSON。 PHP具有编写JSON的功能,javascript可以解码此类JSON对象并创建本机对象

#3


2  

You can use the split() Method in javascript :

您可以在javascript中使用split()方法:

http://www.w3schools.com/jsref/jsref_split.asp

http://www.w3schools.com/jsref/jsref_split.asp

#4


2  

I would do this

我会这样做的

<?php
$string = "a => 1, b => 2";
$arr1 = explode(",",$string);
$arr2 = array();
foreach ($arr1 as $str) {
    $arr3 = explode("=>", $str);
    $arr2[trim($arr3[0])] = trim($arr3[1]);
}
?>
<script type="text/javascript">
$(document).ready(function(){
    var myLetters = new Array();
    <?php foreach($arr2 as $letter => $number): ?>
    myLetters['<?php echo $letter; ?>'] = "<?php echo $number; ?>";
    <?php endforeach; ?>
});
</script>

which will output this

这会输出这个

<script type="text/javascript">
$(document).ready(function(){
    var myLetters = new Array();
        myLetters['a'] = "1";
        myLetters['b'] = "2";
    });
</script>

#1


3  

PHP Code

(for instance, a file, like array.php):

(例如,一个文件,如array.php):

$array=array(
    'a'=>'I am an A',
    'param2_customname'=>$_POST['param2'],
    'param1'=>$_POST['param1']
);
echo json_encode($array);

jQuery code:

$.post('array.php', {'param1': 'lol', 'param2': 'helloworld'}, function(d){
   alert(d.a);
   alert(d.param2_customname);
   alert(d.param1);
},'json');

You can use this for arrays, remember the 'json' tag at the end of the request everytime.

你可以将它用于数组,每次都记住请求末尾的'json'标记。

#2


6  

You should look into JSON. PHP has functions to write JSON and javascript can decode such JSON objects and create native objects

你应该研究一下JSON。 PHP具有编写JSON的功能,javascript可以解码此类JSON对象并创建本机对象

#3


2  

You can use the split() Method in javascript :

您可以在javascript中使用split()方法:

http://www.w3schools.com/jsref/jsref_split.asp

http://www.w3schools.com/jsref/jsref_split.asp

#4


2  

I would do this

我会这样做的

<?php
$string = "a => 1, b => 2";
$arr1 = explode(",",$string);
$arr2 = array();
foreach ($arr1 as $str) {
    $arr3 = explode("=>", $str);
    $arr2[trim($arr3[0])] = trim($arr3[1]);
}
?>
<script type="text/javascript">
$(document).ready(function(){
    var myLetters = new Array();
    <?php foreach($arr2 as $letter => $number): ?>
    myLetters['<?php echo $letter; ?>'] = "<?php echo $number; ?>";
    <?php endforeach; ?>
});
</script>

which will output this

这会输出这个

<script type="text/javascript">
$(document).ready(function(){
    var myLetters = new Array();
        myLetters['a'] = "1";
        myLetters['b'] = "2";
    });
</script>