大字符串到漂亮的数组
我需要将大字符串变成一个漂亮的数组。字符串本身是标签和标签 ID 的列表。它们可以有任意数量。以下是字符串示例:29:funny,30:humor,2:lol
- id:tag_name。现在,我在将其转换为数组时遇到问题 - Array ( [29] => fun [30] => humour )
。我可以到达标签所在的部分
数组(
<块引用>[0] = 数组 (
<块引用>[0] = 29
[1] = 有趣
)
[1] = 数组 (
<块引用>[0] = 30
[1]=幽默
)
)
我也研究过数组函数,但似乎它们都不能帮助我。 有人可以帮我吗?
I need to make big string into a nice array. String itself is list of tags and tag ids. There can be any amount of them. Here is example of the string: 29:funny,30:humor,2:lol
- id:tag_name. Now, I have problem converting it to array - Array ( [29] => funny [30] => humor )
. I can get to the part where tags are as so
Array (
[0] = Array (
[0] = 29
[1] = funny
)
[1] = Array (
[0] = 30
[1] = humor
)
)
I've look at array functions too but seems none of them could help me.
Can anyone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
下面是一些帮助您继续操作的代码:
例如,您可以将 foreach 替换为 array_map ,但我认为这种方式对您来说更简单。
这是它的工作示例: http://codepad.org/4BpnCiEJ
Here's some code to get you going:
You could replace the foreach with an
array_map
for example, but I think it's simpler for you this way.Here's an example of it working: http://codepad.org/4BpnCiEJ
您可以使用explode() 来执行此操作,尽管这需要两次传递。第一个将字符串拆分为配对(explode (',', $string)),第二个将每个配对拆分
You could use explode() to do this, though it would take two passes. The first to split the string into pairings (explode (',', $string)) and the second to split each paring
您可以使用
preg_match_all
输出:
You can use
preg_match_all
Outputs: