如何在客户端存储一个php数组? [英] How to store a php array on the client side?

查看:112
本文介绍了如何在客户端存储一个php数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个mysql数据库中拉一个数组。一个非常简单的选择语句,然后将其放入到运行的多维数组(行和列)中以生成一些HTML。到目前为止这么好。



我希望能够将该数组存储在客户端的某个地方(DOM?),以便客户端可以重新使用和操作该数据(通过jQuery),而不必每次都重新查询相同数据的数据库。



所以...让我说我的php数组是:

  $ fruits = array(fruits=> array(a=>orange,
b= >香蕉,
c=>apple
),
numbers=>数组(1,
2,
3 ,
4,
5,
6
),
holes=> array(first,
5 =>secon d,
third

);

如何将 $ fruits 客户端,所以我可以通过javascript重新使用命令的数据?

解决方案

如何将其存储为一个简单的JavaScript对象?如下所示:

  // yourphpfile.php 
< script type =text / javascript>
var obj = JSON.parse('<?php echo json_encode($ my_arr,JSON_HEX_TAG | JSON_HEX_APOS);?>');
console.log(obj);
< / script>

客户端将把整个数据放入 obj 对象。


I am pulling an array from a mysql database. A pretty simple select statement that then gets put into your run-of-the-mill multi-dimension array (rows and columns) to generate some HTML. So far so good.

I want to be able to store that array somewhere (DOM?) on the client-side so that clients can re-use and manipulate that data (via jQuery) without having to re-query the database for the same data each time.

So... lets say my php array is:

$fruits = array ( "fruits"  => array ( "a" => "orange",
                                       "b" => "banana",
                                       "c" => "apple"
                                     ),
                  "numbers" => array ( 1,
                                       2,
                                       3,
                                       4,
                                       5,
                                       6
                                     ),
                  "holes"   => array (      "first",
                                       5 => "second",
                                            "third"
                                     )
                );

How do I store $fruits on the client-side so I can re-use the data on command via javascript?

解决方案

How about storing it as a simple javascript object? Something like the folllowing:

//yourphpfile.php
<script type="text/javascript">
var obj = JSON.parse('<?php echo json_encode($my_arr,JSON_HEX_TAG|JSON_HEX_APOS); ?>');
console.log(obj);
</script>

The client side will have the entire data into the obj object.

这篇关于如何在客户端存储一个php数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆