简体   繁体   English

如何使用PHP将MySql值存储为多维数组

[英]How to store MySql values as Multidimensional Array using PHP

I have a database table as follows. 我有一个数据库表,如下所示。

<table border='1'><th>Id</th><th>FirstName</th><th>last Name</th><tr><td>1</td><td>Tom</td><td>T</td></tr><tr><td>2</td><td>Jerry</td><td>J</td></tr></table>

I would like to store all values as a multi dimensional array using php(using a while loop to retrieve fields).That is, I would like the data to be echoed as: 我想使用php将所有值存储为多维数组(使用while循环检索字段),也就是说,我希望将数据回显为:

array(array(1,Tom,T),array(2,Jerry,J));
$result = mysql_query("SELECT * FROM tablename;");
while($result_ar = mysql_fetch_array($result)) {
    $multid_array[] = $result_ar;
}

after which $multid_array will be an array of arrays. 之后,$ multid_array将是一个数组数组。

You can use phps serialize function to convert any variable into a string representation 您可以使用phps序列化函数将任何变量转换为字符串表示形式

$string = serialize($dbData);

You can use unserialize() to convert the string back into arrays, objects, etc 您可以使用unserialize()将字符串转换回数组,对象等

$dbData = unserialize($string);

Once you have the data within a string, it's very easy to store in a file, db, etc. A disadvantage is that you won't be able to search your database easily for this data. 一旦将数据包含在字符串中,就很容易将其存储在文件,db等中。缺点是您将无法轻松地在数据库中搜索该数据。

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

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