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

发布于 2024-09-17 00:21:54 字数 476 浏览 3 评论 0原文

我有一个数据库表如下。

<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>

我想使用 php 将所有值存储为多维数组(使用 while 循环来检索字段)。也就是说, 我希望数据被回显为:

array(array(1,Tom,T),array(2,Jerry,J));

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:

array(array(1,Tom,T),array(2,Jerry,J));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-09-24 00:22:32

您可以使用phps序列化函数将任何变量转换为字符串表示形式

$string = serialize($dbData);

您可以使用unserialize()将字符串转换回数组,对象等

$dbData = unserialize($string);

一旦您在字符串中获得了数据,就可以很容易地将其存储在文件,数据库中等等。缺点是您无法轻松地在数据库中搜索这些数据。

You can use phps serialize function to convert any variable into a string representation

$string = serialize($dbData);

You can use unserialize() to convert the string back into arrays, objects, etc

$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.

笑红尘 2024-09-24 00:22:25
$result = mysql_query("SELECT * FROM tablename;");
while($result_ar = mysql_fetch_array($result)) {
    $multid_array[] = $result_ar;
}

之后 $multid_array 将是一个数组的数组。

$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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文