PHP 二维关联数组内爆
我想将以下数组存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您不需要指定有序的数字键,只需编写:
其次,为什么要将整个数组存储为字符串?为什么不将其每个字段存储为 s 列呢?这不是一个选择吗?
第三,您可以在数据库中存储序列化数组(或任何数据)。
但是,如果您想将其存储为直线字符串,那就有点复杂了。您必须循环遍历数组并分别附加键和值。
First, you don't need to specify ordered numeric keys, just write:
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.
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.
您可以使用 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 useunserialize
to convert it back to the array.肯定有更好的解决方案。
将此数据作为单独的记录存储在专用表中。
这就是关系数据库的用途。
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.
你考虑过 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.