双重爆炸数组 [英] Double explode an array

查看:65
本文介绍了双重爆炸数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串:

user:hello,user2:world

所需的输出:

$string = array(
   1 => array( 1 => "user", 2 => "hello"),
   2 => array( 1 => "user2", 2 => "world")
);

我尝试过的方法(无效):

What I've tried (that doesn't work):

$string = explode(',',$string);
$string = explode(':',$string);

我得到的错误: explode()期望参数2为字符串

如何从字符串到所需的输出?谢谢!

The error I get: explode() expects parameter 2 to be string
How can I get from the string to the desired output? Thanks!

推荐答案

循环遍历第一次爆炸的输出,并第二次爆炸每个值。

loop over the output from the first explode and explode the second time on each value.

$string = "user:hello,user2:world";
$array = explode(',', $string);

foreach($array as $k=>$v){
    $array[$k] = explode(':', $v);
}

这篇关于双重爆炸数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆