PHP中如何将逗号和分隔符分隔的字符串转换为有组织的数组


How to conver this comma and delimiter seperated string into organized array in PHP?

我正在寻找一种方法来转换这个字符串:

term:entertainment,taxonomy:category,term:lifestyle,taxonomy:category

到这个PHP数组中(有组织的,没有重复的分类法)

Array ( [category] => entertainment,lifestyle ) 
$splitted = explode(",", $string);
for ($i = 0; $i < count($splitted); $i+=2) {
    $result = explode(":",$splitted[$i])[1]; // (PHP 5.4 syntax)
    $key = explode(":",$splitted[$i+1])[1];
    if (isset($array[$key]))
        $array[$key] .= ",$result";
    else
        $array[$key] = $result;
}

首先用逗号分隔,然后将数据添加到以分号分隔的数组中。

(当您的字符串总是具有相同的形式时,这就足够了)