JavaScript array from PHP string - Stack Overflow

I am developing an application having more than thousands of values. I am trying to make a dynamic arra

I am developing an application having more than thousands of values. I am trying to make a dynamic array in JavaScript. I'm using AJAX to get my values. So I have to create a string from PHP ,it should able to convert from string to array in JavaScript.

How can I make a string in PHP that can be converted to array in JavaScript?

I am developing an application having more than thousands of values. I am trying to make a dynamic array in JavaScript. I'm using AJAX to get my values. So I have to create a string from PHP ,it should able to convert from string to array in JavaScript.

How can I make a string in PHP that can be converted to array in JavaScript?

Share Improve this question edited Mar 12, 2012 at 10:30 Fenton 251k73 gold badges401 silver badges409 bronze badges asked Mar 12, 2012 at 10:27 user2743235user2743235 2
  • you should make json string in PHP and then json to JS object for javascript logic. json/js.html – Riz Commented Mar 12, 2012 at 10:30
  • 1 This isn't hard to do (json_encode is your friend), but "more than thousands of values" suggests you might want to rethink your application architecture... – lonesomeday Commented Mar 12, 2012 at 10:30
Add a ment  | 

3 Answers 3

Reset to default 8

You are looking for JSON and json_encode():

$string = json_encode($array);

The content of the string will be the array written in valid javascript.

Use JSON notation to create a string of values and then read it from JS. The simplest way to do this is something like that:

<script type="text/javascript">
var myPHPData = <?php echo json_encode($myData); ?>;
alert myPHPData; // now you have access to it in JS
</script>

if you dont intend to use json , you can do something of this kind..

you can create a string of this kind in php (separate it by any delimiter)

      $arr = "1;2;3;4;5;6;7" ;

in javascript you can convert this to array using split function

      //make an ajax call and get this string (say str)
      arr = str.split(";");

split() returns an array

 arr[0] is 1  
 arr[1] is 2
 and so on !!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742277753a4413907.html

相关推荐

  • JavaScript array from PHP string - Stack Overflow

    I am developing an application having more than thousands of values. I am trying to make a dynamic arra

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信