Reputation: 15197
How do I implode 2 values, 1 as the key and the other as the value. Say I have:
$string = 'hello_world';
$arg = explode('_', $string);
I now have $arg[0]
and $arg[1]
(as you know)
How can I implode that so it becomes this structure
Array (
'hello' => 'world'
)
Upvotes: 1
Views: 321
Reputation: 606
Here's a fun way to do it without using intermediate args ;)
$string = "hello_world";
$result = call_user_func_array( "array_combine", array_chunk( explode("_", $string ), 1 ));
Upvotes: 4
Reputation: 36956
$string = 'hello_world';
$arg = explode('_', $string);
$array = array($arg[0] => $arg[1]);
would be the quickest way
Upvotes: 2
Reputation: 15905
I'm not sure if you're looking for something this obvious:
$arg = explode('_', 'hello_world');
print_r(array($arg[0] => $arg[1]));
I assume it's a bit more complicated than this. Maybe the string contains multiple of these things. ex: 'hello_world,foo_bar,stack_overflow'. In which case you'd need to explode by the comma first:
$args = explode(',', 'hello_world,foo_bar,stack_overflow');
$parsed = array();
foreach($args as $arg) {
list($key, $value) = explode('_', $arg);
$parsed[$key] = $value;
}
Upvotes: 3