PHP在将URL分解为多维数组后保留名称
如果你否决了我的问题,请留下理由 嘿,伙计们,我得到了一个小剪报,它将采用这样的URL结构PHP在将URL分解为多维数组后保留名称,php,multidimensional-array,array-merge,Php,Multidimensional Array,Array Merge,如果你否决了我的问题,请留下理由 嘿,伙计们,我得到了一个小剪报,它将采用这样的URL结构 // brevity [182] => Array ( [path] => /home-phone/troubleshooting [name] => Troubleshooting ) [183] => Array ( [path] => /home-phone/troubleshooting/my-
// brevity
[182] => Array
(
[path] => /home-phone/troubleshooting
[name] => Troubleshooting
)
[183] => Array
(
[path] => /home-phone/troubleshooting/my-voicemail-to-e-mail-feature-is-not-working
[name] => My Voicemail to E-mail feature is not working.
)
[184] => Array
(
[path] => /home-phone/troubleshooting/when-i-receive-my-voicemail-by-e-mail-i-cannot-hear-the-message
[name] => When I receive my voicemail by e-mail, I cannot hear the message.
)
[185] => Array
(
[path] => /home-phone/troubleshooting/i-don-t-hear-a-dial-tone-when-i-pick-up-my-phone
[name] => I don't hear a dial tone when I pick up my phone.
)
[186] => Array
(
[path] => /home-phone/troubleshooting/i-moved-my-voice-adapter-and-can-no-longer-make-or-receive-calls
[name] => I moved my Voice adapter and can no longer make or receive calls.
)
[187] => Array
(
[path] => /home-phone/troubleshooting/my-call-waiting-is-not-working-since-i-forwarded-voice-to-another-line
[name] => My call waiting is not working since I forwarded Voice to another line.
)
[188] => Array
(
[path] => /home-phone/troubleshooting/i-m-unable-to-complete-transactions-on-ivr-phone-calls-like-online-banking
[name] => I'm unable to complete transactions on IVR phone calls (like online banking).
)
[189] => Array
(
[path] => /home-phone/troubleshooting/i-can-t-get-a-dial-tone-and-i-could-before
[name] => I can't get a dial tone (and I could before).
)
[190] => Array
(
[path] => /home-phone/troubleshooting/im-not-getting-my-voicemail-to-e-mail-messages
[name] => I'm not getting my Voicemail to E-mail messages.
)
[191] => Array
(
[path] => /home-phone/troubleshooting/my-caller-id-has-not-worked-since-i-forwarded-my-voice-line-to-another-phone-line
[name] => My Caller ID has not worked since I forwarded my Voice line to another phone line.
)
[192] => Array
(
[path] => /home-phone/troubleshooting/im-having-trouble-sending-andor-receiving-a-fax
[name] => I'm having trouble sending and/or receiving a FAX.
)
// brevity
然后把它们变成一个多维数组,像这样
[4] => home-phone
[home-phone] => Array
(
// brevity
[9] => troubleshooting
[troubleshooting] => Array
(
[0] => my-voicemail-to-e-mail-feature-is-not-working
[1] => when-i-receive-my-voicemail-by-e-mail-i-cannot-hear-the-message
[2] => i-don-t-hear-a-dial-tone-when-i-pick-up-my-phone
[3] => i-moved-my-voice-adapter-and-can-no-longer-make-or-receive-calls
[4] => my-call-waiting-is-not-working-since-i-forwarded-voice-to-another-line
[5] => i-m-unable-to-complete-transactions-on-ivr-phone-calls-like-online-banking
[6] => i-can-t-get-a-dial-tone-and-i-could-before
[7] => im-not-getting-my-voicemail-to-e-mail-messages
[8] => my-caller-id-has-not-worked-since-i-forwarded-my-voice-line-to-another-phone-line
[9] => im-having-trouble-sending-andor-receiving-a-fax
)
// brevity
)
效果很好,但我到了最后才意识到我的名字全丢了
(你可以在第二列看到他们的名字),我给名字留了一个空格,
但就我个人而言,我现在不知道怎么把它放进去=(
有什么想法吗
编辑忘记了我的源代码;)
预期产出应为
[Home Phone] => home-phone
[home-phone] => Array
(
// brevity
[Troubleshooting] => troubleshooting
[troubleshooting] => Array
(
[My Voicemail to E-mail feature is not working.] => my-voicemail-to-e-mail-feature-is-not-working
[When I receive my voicemail by e-mail, I cannot hear the message.] => when-i-receive-my-voicemail-by-e-mail-i-cannot-hear-the-message
[I don't hear a dial tone when I pick up my phone.] => i-don-t-hear-a-dial-tone-when-i-pick-up-my-phone
// etc
)
// brevity
)
由于您没有将预期的输出放在假设上,所以我正在工作。。您可以尝试:
$data = array();
$i = 0 ;
foreach ( $array as $key => $value ) {
$temp = array();
setPath($temp,dirname($value['path']) . "/$i/path", basename($value['path']));
setPath($temp, dirname($value['path']) . "/$i/name", $value['name']);
$data = array_merge_recursive($data, $temp);
$i ++;
}
echo "<pre>";
print_r($data);
使用的功能
为什么URL不加引号?它们不是弦吗=/好的,我就快到了。。你能举例说明你的预期产出吗。。。因此,在我发布我的答案之前,我可以确定您是否希望它类似于
数组(“name”=>“my-voicemail-to-e-mail-feature-is-not-working”)代码>???已更新预期输出,很抱歉我回家了一天,因此无法发表评论。
$data = array();
$i = 0 ;
foreach ( $array as $key => $value ) {
$temp = array();
setPath($temp,dirname($value['path']) . "/$i/path", basename($value['path']));
setPath($temp, dirname($value['path']) . "/$i/name", $value['name']);
$data = array_merge_recursive($data, $temp);
$i ++;
}
echo "<pre>";
print_r($data);
Array
(
[home-phone] => Array
(
[path] => troubleshooting
[name] => Troubleshooting
[troubleshooting] => Array
(
[1] => Array
(
[path] => my-voicemail-to-e-mail-feature-is-not-working
[name] => My Voicemail to E-mail feature is not working.
)
[2] => Array
(
[path] => when-i-receive-my-voicemail-by-e-mail-i-cannot-hear-the-message
[name] => When I receive my voicemail by e-mail, I cannot hear the message.
)
[3] => Array
(
[path] => i-don-t-hear-a-dial-tone-when-i-pick-up-my-phone
[name] => I don't hear a dial tone when I pick up my phone.
)
[4] => Array
(
[path] => i-moved-my-voice-adapter-and-can-no-longer-make-or-receive-calls
[name] => I moved my Voice adapter and can no longer make or receive calls.
)
[5] => Array
(
[path] => my-call-waiting-is-not-working-since-i-forwarded-voice-to-another-line
[name] => My call waiting is not working since I forwarded Voice to another line.
)
[6] => Array
(
[path] => i-m-unable-to-complete-transactions-on-ivr-phone-calls-like-online-banking
[name] => I'm unable to complete transactions on IVR phone calls (like online banking).
)
[7] => Array
(
[path] => i-can-t-get-a-dial-tone-and-i-could-before
[name] => I can't get a dial tone (and I could before).
)
[8] => Array
(
[path] => im-not-getting-my-voicemail-to-e-mail-messages
[name] => I'm not getting my Voicemail to E-mail messages.
)
[9] => Array
(
[path] => my-caller-id-has-not-worked-since-i-forwarded-my-voice-line-to-another-phone-line
[name] => My Caller ID has not worked since I forwarded my Voice line to another phone line.
)
[10] => Array
(
[path] => im-having-trouble-sending-andor-receiving-a-fax
[name] => I'm having trouble sending and/or receiving a FAX.
)
)
)
)
function setPath(&$temp, $url, $value) {
foreach ( array_filter(explode("/", $url)) as $key ) {
$temp = &$temp[$key];
}
$temp = $value;
}