如何内爆 2 个值,1 个作为键,另一个作为值。说我有:
$string = 'hello_world';
$arg = explode('_', $string);
我现在有
$arg[0]
和$arg[1]
(如你所知)
我怎样才能将其内爆,使其变成这样的结构
Array (
'hello' => 'world'
)
$array = array($arg[0] => $arg[1]);
这是一种无需使用中间参数即可实现的有趣方法;)
$string = "hello_world";
$result = call_user_func_array( "array_combine", array_chunk( explode("_", $string ), 1 ));
我不确定你是否正在寻找这么明显的东西:
$arg = explode('_', 'hello_world');
print_r(array($arg[0] => $arg[1]));
我认为它比这更复杂一点。也许该字符串包含多个这样的东西。例如:'hello_world,foo_bar,stack_overflow'。在这种情况下,您需要先用逗号进行爆炸:
$args = explode(',', 'hello_world,foo_bar,stack_overflow');
$parsed = array();
foreach($args as $arg) {
list($key, $value) = explode('_', $arg);
$parsed[$key] = $value;
}
$string = 'hello_world';
$arg = explode('_', $string);
$array = array($arg[0] => $arg[1]);
将是最快的方法