-1

如果您对我的问题投反对票,请善意地留下一个理由

嘿,伙计们,我有一个小片段,它将采用这样的网址结构

// 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

并将它们变成一个multidim数组,像这样

[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

    )

工作得很好,但我到最后才意识到我失去了所有的名字(你可以看到他们的第二列名字),我留下了一个名字的间隔,但我一生都想不通现在怎么弄进去=(

有任何想法吗?

编辑忘记了我的源代码;)

    $tree = array();
    foreach($indexArray as $branch) {

        $limb = explode('/', $branch['path']);

        $subTree = array(array_pop($limb));
        foreach (array_reverse($limb) as $dir) {
            $subTree = array($dir => $subTree);
        }

        $tree = array_merge_recursive($tree, $subTree);

    }

预期输出应该是

[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

    )
4

1 回答 1

1

由于您没有将预期的输出放在假设上..您可以尝试:

$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;
}
于 2012-10-18T01:35:27.487 回答