将数组爆炸成已在php

时间:2016-12-19 13:04:37

标签: php arrays associative-array

我的字符串为

SportId : 56,GroundType : Public,SelectArea : 10,Cost : 3000-4000 ,Size : 7 * 7

爆炸时此数组输出为

Array
(
    [0] => SportId : 56
    [1] => GroundType : Public
    [2] => SelectArea : 10
    [3] => Cost : 3000-4000 
    [4] => Size : 7 * 7
)

我希望关联数组中的输出为

 Array
(
    ['SportId'] => 56
    ['GroundType'] => Public
    ['SelectArea'] => 10
    ['Cost'] => 3000-4000 
    ['Size'] => 7 * 7
)

1 个答案:

答案 0 :(得分:0)

这应该做:

<?php

$info = "SportId : 56,GroundType : Public,SelectArea : 10,Cost : 3000-4000 ,Size : 7 * 7";
$arrInfo = explode(",",$info);

$newArray = [];
foreach($arrInfo as $item) {
    $values = explode(":",$item);
    $newArray[$values[0]] = $values[1];
}

print_r($newArray);
相关问题