Deejay Deejay - 2 months ago 7
PHP Question

Fetch Parameters from arg1:value/arg2:value2

I have a string like:-

object_id:56/object:display/role:test


I want to fetch all params from string like


$object_id = 56 and $object = display and so on...


Tried http://php.net/EXPLODE#99167

this gave me an array

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

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

)


I want them as


$object_id = 56
$object = display

Answer

Just explode them, first by / forward slashes, then explode again the exploded pieces by : colon:

$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

Comments