从arg1:value / arg2:value2获取参数

时间:2015-02-26 08:15:47

标签: php arrays

我有一个字符串: -

object_id:56/object:display/role:test

我想从字符串中获取所有参数

  

$ object_id = 56和$ object = display等等......

尝试http://php.net/EXPLODE#99167

这给了我一个阵列

Array
(
    [0] => Array
        (
            [0] => object_id
            [1] => 56
        )

    [1] => Array
        (
            [0] => object
            [1] => display
        )
    [2] => Array
        (
            [0] => role
            [1] => test
        )

)

我希望他们为

  

$ object_id = 56   $ object = display

1 个答案:

答案 0 :(得分:2)

首先通过/正斜杠展开它们,然后再通过:冒号爆炸爆炸的碎片:

$string = 'object_id:56/object:display/role:test';
foreach(explode('/', $string) as $val1) {
    list($key, $val) = explode(':', $val1);
    $$key = $val;
}

echo $object_id , '<br/>', $object , '<br/>', $role;

Sample Output