将数据分成数组

时间:2017-08-10 02:59:35

标签: php arrays

结果:

337:230

323:112

代码:

$option = explode(",",$this->request->get['opt']);

foreach ($option as $key => $value) {
    echo $value ."<br>";        
}

问题:如何将上述数据分成数组,如 [337] =&gt; 230,[323] =&gt; 112

1 个答案:

答案 0 :(得分:1)

看起来你只需要在冒号上再次爆炸,所以也许像this这样的东西?

<?php

$opt = '337:230,323:112';

$option = explode(",", $opt);

foreach ($option as $pair) {
    list(key, $value) = explode(':', $pair);
    $array[$key] = $value;
}

print_r($array);
Array
(
    [337] => 230
    [323] => 112
)