开发者

Explode data / transform it to the PHP array

My data variable is following:

 canv = 2,3,4,5:::canp = 2,3,4:::canpr = 2,3,4:::canpp = 2,3,4:::all = 2,3,4

I need it to transform to an array which should look following:

$arr = array(
"canv" => array("2", "3", "4", "5"),
"canp" => array("2", "3", "4")开发者_高级运维,
"canpr" => array("2", "3", "4"),
"canpp" => array("2", "3", "4"),
"all" => array("2", "3", "4")
);

Can you help me?


$data = "canv = 2,3,4,5:::canp = 2,3,4:::canpr = 2,3,4:::canpp = 2,3,4:::all = 2,3,4";
$result = array();
foreach (explode(':::', $data) as $line) {
    list($part1, $part2) = explode(' = ', $line);
    $result[$part1] = explode(',', $part2);
}


The following should do the trick:

$data = "canv = 2,3,4,5:::canp = 2,3,4:::canpr = 2,3,4:::canpp = 2,3,4:::all = 2,3,4";
$items = explode(":::", $data);
$arr = array();
foreach($items as $item) {
    $item = explode(" = ", $item);
    $arr[$item[0]] = explode(",", $item[1]);
}


$orig_str = 'canv = 2,3,4,5:::canp = 2,3,4:::canpr = 2,3,4:::canpp = 2,3,4:::all = 2,3,4';

$parts = explode(':::', $orig_str);

$data = array()
foreach($parts as $key => $subparts) {
    $data[$key] = explode(',', $subparts);
}


I would try something like this: this is not tested, try to print_r to debug

$string = "canv = 2,3,4,5:::canp = 2,3,4:::canpr = 2,3,4:::canpp = 2,3,4:::all = 2,3,4";

$pieces = explode(':::',$string);
$result = array();
foreach($pieces AS $piece)
{
   $tmp = explode(' = ',$piece);
   $result[$tmp[0]] = explode(',',$tmp[1]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜