PHP 二维关联数组内爆

发布于 2024-09-26 22:12:57 字数 2169 浏览 0 评论 0原文

我想将以下数组存储在 mySQL 表中的单个文本字段中:

$user_rating[0] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 1', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[1] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 2', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[2] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 3', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[3] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 4', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[4] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 5', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[5] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 6', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[6] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 7', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

我相信我还需要内爆关联数组才能完成此任务,但遇到了一些麻烦。

也欢迎任何其他/更好的阵列结构解决方案。

谢谢

I'd like to store the following array in a single text field in a mySQL table:

$user_rating[0] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 1', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[1] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 2', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[2] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 3', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[3] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 4', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[4] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 5', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[5] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 6', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

$user_rating[6] = array('percent' => 'XXXXXXXXXXXXXXXXX', 
                    'category' => 'Category 7', 
                    'description' => 'XXXXXXXXXXXXXXXXX',
                    'link' => 'XXXXXXXXXXXXXXXXX',
                    'thumbnail' => 'XXXXXXXXXXXXXXXXX');

I believe I need to implode the associative arrays as well to accomplish this but am having some trouble.

Any other/better array structure solutions are also welcomed.

Thanks

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

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

发布评论

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

评论(4

雨轻弹 2024-10-03 22:12:57

首先,您不需要指定有序的数字键,只需编写:

$user_rating[] = array();

其次,为什么要将整个数组存储为字符串?为什么不将其每个字段存储为 s 列呢?这不是一个选择吗?

第三,您可以在数据库中存储序列化数组(或任何数据)。

serialize($user_rating);
store_to_db();
...
$handler = unserialize(get_data_out());

但是,如果您想将其存储为直线字符串,那就有点复杂了。您必须循环遍历数组并分别附加键和值。

First, you don't need to specify ordered numeric keys, just write:

$user_rating[] = array();

Second, why would you want to store the entire array as a string? Why not just store each of its fields as s column? Is this not an option?

Third, you can store serialized arrays (or any data, for that matter) in the DB.

serialize($user_rating);
store_to_db();
...
$handler = unserialize(get_data_out());

If you want to store it as a straight up string, however, that's a bit more complicated. You would have to cycle through the array and append the keys and values individually.

彻夜缠绵 2024-10-03 22:12:57

您可以使用 PHP 的 serialize 将数组转换为单个字符串。当您从数据库检索它时,可以使用 unserialize 将其转换回数组。

You can use PHP's serialize to convert the array to a single string. When you retrieve it from the database you can use unserialize to convert it back to the array.

安人多梦 2024-10-03 22:12:57

肯定有更好的解决方案。
将此数据作为单独的记录存储在专用表中。
这就是关系数据库的用途。

There is a better solution for sure.
Store this data as a separate records in a dedicated table.
That's what relational databases are for.

红衣飘飘貌似仙 2024-10-03 22:12:57

你考虑过 JSON 吗?如果您想坚持单一领域,这可能是您最好的选择。

PHP - JavaScript 对象表示法

编辑: 甚至更好,阅读(并投票)下面的评论,因为 serialize 是一个本机函数。

Have you considered JSON? It might be your best bet if you want to stick to a single field.

PHP - JavaScript Object Notation

edit: Or even better, read (and vote) for the comment below, because serialize is a native function.

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