大字符串到漂亮的数组

发布于 2024-12-21 04:49:28 字数 588 浏览 1 评论 0原文

我需要将大字符串变成一个漂亮的数组。字符串本身是标签和标签 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 技术交流群。

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

发布评论

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

评论(5

吐个泡泡 2024-12-28 04:49:28

下面是一些帮助您继续操作的代码:

$str = "29:funny,30:humor,2:lol";
$arr = array();
foreach (explode(',', $str) as $v) {
    list($key, $val) = explode(':', $v);
    $arr[$key] = $val;
}
print_r($arr);
/* will output:
Array
(
    [29] => funny
    [30] => humor
    [2] => lol
)
*/

例如,您可以将 foreach 替换为 array_map ,但我认为这种方式对您来说更简单。

这是它的工作示例: http://codepad.org/4BpnCiEJ

Here's some code to get you going:

$str = "29:funny,30:humor,2:lol";
$arr = array();
foreach (explode(',', $str) as $v) {
    list($key, $val) = explode(':', $v);
    $arr[$key] = $val;
}
print_r($arr);
/* will output:
Array
(
    [29] => funny
    [30] => humor
    [2] => lol
)
*/

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

避讳 2024-12-28 04:49:28

您可以使用explode() 来执行此操作,尽管这需要两次传递。第一个将字符串拆分为配对(explode (',', $string)),第二个将每个配对拆分

$arr = explode (',', $string);
foreach ($arr as &$pairing)
{
    $pairing = explode (':', $pairing);
}

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

$arr = explode (',', $string);
foreach ($arr as &$pairing)
{
    $pairing = explode (':', $pairing);
}
┾廆蒐ゝ 2024-12-28 04:49:28
$string = '29:funny,30:humor,2:lol';
$arr1   = explode(',', $string);
$result = array();
foreach ($arr1 as $element1) {
    $result[]  = explode(':', $element1);
}
print_r($result);
$string = '29:funny,30:humor,2:lol';
$arr1   = explode(',', $string);
$result = array();
foreach ($arr1 as $element1) {
    $result[]  = explode(':', $element1);
}
print_r($result);
燕归巢 2024-12-28 04:49:28

您可以使用 preg_match_all

preg_match_all('#([\d]+):([a-zA-Z0-9]+)#', $sString, $aMatches);

// Combine the keys with the values.
$aArray = array_combine($aMatches[1], $aMatches[2]);

echo "<pre>";
print_r($aArray);
echo "</pre>";

输出:

Array
(
    [29] => funny
    [30] => humor
    [2] => lol
)

You can use preg_match_all

preg_match_all('#([\d]+):([a-zA-Z0-9]+)#', $sString, $aMatches);

// Combine the keys with the values.
$aArray = array_combine($aMatches[1], $aMatches[2]);

echo "<pre>";
print_r($aArray);
echo "</pre>";

Outputs:

Array
(
    [29] => funny
    [30] => humor
    [2] => lol
)
鸩远一方 2024-12-28 04:49:28
<?php

$test = '29:funny,30:humor,2:lol';

$tmp_array = explode(',', $test);

$tag_array = ARRAY();

foreach ($tmp_array AS $value) {
    $pair = explode(':', $value);
    $tag_array[$pair[0]] = $pair[1];
}

var_dump($tag_array);

?>
<?php

$test = '29:funny,30:humor,2:lol';

$tmp_array = explode(',', $test);

$tag_array = ARRAY();

foreach ($tmp_array AS $value) {
    $pair = explode(':', $value);
    $tag_array[$pair[0]] = $pair[1];
}

var_dump($tag_array);

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