Search code examples
phpmultidimensional-arrayarray-merge

PHP keeping Names after breaking urls into a multidimensional array


If you downvote my question, please be KIND enough to leave a reason

Hey guys I got a little snip that will take a structure of urls like so

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

And turn them into a multidim array, like this

[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

    )

Works pretty good, but I got to the end and realized I'd lost all my Names (your can see theirs a second column for names), I left a spacer for a Name, but for the life of me can't figure out how to get it in there now =(

Any ideas?

EDIT forgot my source code ;)

    $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);

    }

Expected output should be

[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

    )

Solution

  • Since you did not put expected output am working on assumption .. you can try :

    $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);
    

    Output

    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 Used

    function setPath(&$temp, $url, $value) {
        foreach ( array_filter(explode("/", $url)) as $key ) {
            $temp = &$temp[$key];
        }
        $temp = $value;
    }