我正在研究一个应该解析 url 以创建关联数组的函数。
我无法用正确的字符串等效替换数字键。
例如,http://localhost/fr/user/edit/5/?foo=bar&love=hate
产生
Array
(
[langue] => fr
[app] => user
[action] => edit
[id] => 5
[0] => user
[1] => 5
[foo] => bar
[love] => hate
)
我想要的是:
Array
(
[langue] => fr
[app] => user
[action] => edit
[id] => 5
[foo] => bar
[love] => hate
)
到目前为止,这是我的功能:
<?php
$url = parse_url($_SERVER['REQUEST_URI']);
print_r($url);
// 1./ "folder path" into array
$values = explode('/', $url['path']);
unset($values[0]);
// 2./ "url variables" into array
parse_str($url['query'], $vars);
$url = array_merge($values,$vars);
// 3./ remove empty values
$url = array_filter($url);
print_r($url);
// 4./ replace numeric keys by the application vars
$keys = array('langue','app','action','id');
$count = 0;
foreach($url as $key => $value)
{
if(!is_string($key))
{
$first_array = array_splice ($url, 0,$count);
$insert_array = [$keys[$key] => $value ];
unset($url[$key]);
$url = array_merge ($first_array, $insert_array, $url);
}
$count++;
}
print_r($url);