从列表构建数组

时间:2013-09-03 13:18:49

标签: php arrays

我如何构建以下字符串的数组:

$scope = "calls:full,departments:full,employees:full,companies:full,stages:full,accounts:full,resources:full,products:full,reports:full";

代表它如此:

$scope = array(
    'calls' => full,
    'departments' => full,
    'employees' => full,
    .... and so on >>
);

3 个答案:

答案 0 :(得分:5)

$scope = "calls:full,departments:full,employees:full,companies:full,stages:full,accounts:full,resources:full,products:full,reports:full";
$parts = explode(',', $scope);
$arr = array();

foreach($parts as $val)   {
    list($key, $value) = explode(':', $val);
    $arr[$key] = $value; 
}

答案 1 :(得分:0)

$scope = "calls:full,departments:full,employees:full,companies:full,stages:full,accounts:full,resources:full,products:full,reports:full";
foreach(explode(",", $scope) as $v){ 
  $temp = explode(":", $v); 
  $array[$temp[0]]=$temp[1];
}
print_r($array);

答案 2 :(得分:0)

<?php
$a = "calls:full,departments:full,employees:full,companies:full,stages:full,accounts:full,resources:full,products:full,reports:full";
$exploded=array();
$exploded=explode(",",$a);

$new=array();
foreach($exploded as $explode){
    $explode=explode(":",$explode);
    //print_r($explode);
    $new[$explode[0]]=$explode[1];
}
print_r($new);
?>