1

I have been looking how to do this and am a bit stumped. My array is as follows:

$returndata->setup_array = array(
            'General'       =>  array(
                    'Main Details'          => 'setup/maindets',
                    'Directories'           => 'directories',
                    'Extension Allocation'  => 'xtnallo',
                    'List Holidays'         => 'setup/holidays',
                    'List Group VM'         => 'groupvm',
                    'Conference Rooms'      => 'confroom'
                    ),
            'Offices'       =>  array(
                    'List Offices'          => 'iptoffices'
                    ),
            'Users'         =>  array(
                    'List Users'            => 'iptusers'
                    ),
            'Phones'        =>  array(
                    'List Phones'           => 'iptphones'
                    ),
            );

However I have 1 item that on a certain condition(triggered by the users session) that needs to be added to the listin the general array. The section being 'View Details => setup/viewdetails'. I have tried array push (probably incorrectly) but this adds the item as another array at the end under the main array.

I want/need it to work like this:

$returndata->setup_array = array(
        'General'       =>  array(
                 $viewdets
                'Main Details'          => 'setup/maindets',
                'Directories'           => 'directories',
                'Extension Allocation'  => 'xtnallo',
                'List Holidays'         => 'setup/holidays',
                'List Group VM'         => 'groupvm',
                'Conference Rooms'      => 'confroom'
                ),
        'Offices'       =>  array(
                'List Offices'          => 'iptoffices'
                ),
        'Users'         =>  array(
                'List Users'            => 'iptusers'
                ),
        'Phones'        =>  array(
                'List Phones'           => 'iptphones'
                ),
        );

$viewdets = "'View Details'         => 'setup/viewdetails'";

and still be interpreted as a functioning array for use as a menu.

1
  • Maybe create the setup array as is but call it $defaults. Then based on your logic build an array the mirrors that structure and finally use $returndata->setup_array = array_merge_recursive($userData, $defaults); Hopefully this will show you some love. Commented May 10, 2013 at 15:51

3 Answers 3

2
$returndata->setup_array['General']['View Details'] = 'setup/viewdetails'

Cheers Rick!

Sign up to request clarification or add additional context in comments.

2 Comments

close. Should be $returndata->setup_array['General']['View Details'] = 'setup/viewdetails'
You should edit the correct array path into your answer Alex just for clarity. I've upvoted it since it's quite obviously the easiest answer with the least amount of code to get the same result.
0

You can use ArrayObject to have the array as a reference:

$a  = new ArrayObject();

$b = array(
    "a" => $a
);

$a[] = "foo";
print_r($b);

Comments

0

What did you try calling array_push() on? Have you tried

array_push($returndata->setup_array['General'], $viewdets);

You would need to add the variable to the specific depth of the array you wanted it to be present. check out array_push here, there's also a short language syntax that avoids the function call:

$returndata->setup_array['General'][] = $viewdets;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.