简体   繁体   English

从Javascript中的多维PHP数组获取值

[英]Get Values From Multidimensional PHP Array inside Javascript

Here's my situation: 这是我的情况:

I have a MYSQL database with 2 fields: 'ID' and 'string' . 我有一个包含2个字段的MYSQL数据库: 'ID''string' The 'string' field stores strings in form of serialized arrays. “字符串”字段以序列化数组的形式存储字符串 So, to extract it back, I use this PHP code: 因此,要将其提取回来,我使用以下PHP代码:

$rez=mysql_query("SELECT * FROM drawings") or die(mysql_error());

$array=array();

while($row=mysql_fetch_array($rez))
{
    $array[]=unserialize($row['string']);
}

Now my $array variable should contain an array of arrays, right? 现在我的$ array变量应该包含一个数组数组,对吗? Assuming I did that correctly, I then echo $array; 假设我做的正确,然后echo $array; so my ajax call catches it inside the return_data, like this: 所以我的ajax调用将它捕获在return_data内,如下所示:

var return_data = hr.responseText;

Just to test if I can extract a value, I then tried implementing the following code, but it doesn't seem to work: 为了测试是否可以提取值,我尝试实现以下代码,但似乎不起作用:

var arr = return_data.split(",");
var sub_arr = arr[0].split(",");
alert(sub_arr[0]);

What am I doing wrong? 我究竟做错了什么?

Additional info: 附加信息:

I am basically storing a bunch of coordinates inside the MYSQL database, each row being a separate array of coordinates, for example (12,13,14,16,17,20) - these would be 3 "points" contained in one row. 我基本上是在MYSQL数据库中存储一堆坐标,每行是一个单独的坐标数组,例如(12,13,14,16,17,20)-这些将包含在一行中的3个“点”。

Then I'm using an ajax call to extract all the records from the database in a form of array, which contains arrays of multiple numbers (I know that each 2 neighboring numbers make up one point). 然后,我使用ajax调用以数组的形式从数据库中提取所有记录,该数组包含多个数字的数组(我知道每个相邻的2个数字组成一个点)。

EDIT: 编辑:

The chrome javascript console outputs: chrome javascript控制台输出:

Error in event handler for 'undefined': Cannot read property 'disable_serps_pos_numbers' of undefined TypeError: Cannot read property 'disable_serps_pos_numbers' of undefined at BasePlugin.GooglePlugin.getSomeCorePrefs (chrome-extension://akdgnmcogleenhbclghghlkkdndkjdjc/content_scripts/google.js:129:48) at chrome-extension://akdgnmcogleenhbclghghlkkdndkjdjc/lib/lib.js:44:25 at miscellaneous_bindings:287:9 at Event.dispatchToListener (event_bindings:356:21) at Event.dispatch_ (event_bindings:342:27) at Event.dispatch (event_bindings:362:17) at Object.chromeHidden.Port.dispatchOnMessage (miscellaneous_bindings:253:22) 事件处理程序中“未定义”的错误:无法读取未定义Type的属性“ disable_serps_pos_numbers”:无法在BasePlugin.GooglePlugin.getSomeCorePrefs(chrome-extension:// akdgnmcogleenhbclghlklkd_js:129 /。 48)在chrome-extension://akdgnmcogleenhbclghghlkkdndkjdjc/lib/lib.js:44:25在miscellaneous_bindings:287:9在Event.dispatchToListener(event_bindings:356:21)在Event.dispatch_(event_bindings:342:27) .dispatch(event_bindings:362:17)位于Object.chromeHidden.Port.dispatchOnMessage(miscellaneous_bindings:253:22)

while($row=mysql_fetch_array($rez))
{
    $array[]=unserialize($row['string']);
}

echo json_encode($array);

Then in the Javascript code (assuming jQuery): 然后在Javascript代码中(假设使用jQuery):

$.parseJSON(return_data)

Then you can use it like a 2D array in Javascript. 然后,您可以像在Javascript中使用2D数组一样使用它。

Use the standard functions that come with jQuery and PHP to convert arrays into JSON data and back to array. 使用jQuery和PHP随附的标准函数将数组转换为JSON数据并返回数组。

Like json_encode() and json_decode() in PHP 像PHP中的json_encode()和json_decode()

and JS: Parse JSON in JavaScript? 和JS: 在JavaScript中解析JSON?

First, I strongly recommend you switch to using PDO for SQL queries, but to answer your question you said that you are doing: 首先,我强烈建议您切换为SQL查询使用PDO,但是要回答您说的问题,请执行以下操作:

echo $array; echo $ array;

Which will result in an error Array to String Conversion. 这将导致错误的数组到字符串转换。

I recommend using doing echo json_encode($array); 我建议使用echo json_encode($ array); which will convert the array to a json object. 它将数组转换为json对象。

http://php.net/manual/en/function.json-encode.php http://php.net/manual/en/function.json-encode.php

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

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