-1

I have the following function:

        function navigation_permission($permission) {
        array(
        1 => array(
            array('name' => 'members','onclick' => 'test11'),
            array('name' => 'members','onclick' => 'test12'),
            array('name' => 'members','onclick' => 'test13').
            ),
        2 => array(
        array('name' => 'members','onclick' => 'test21'),
        array('name' => 'members','onclick' => 'test22'),
        array('name' => 'members','onclick' => 'test23'),
        )

        5 => array(
        array('name' => 'members','onclick' => 'test34'),
        array('name' => 'members','onclick' => 'test35'),
        )
        );

        // RETURN
    }

the $permission is a number from 0 to 6.

How can i make it able to return the array`s based on the permission and the key value in the array?

Here are some examples for the output:

navigation_permission(6);

would return an array with all the other arrays, example:

    array(
    array('name' => 'members','onclick' => 'test11'),
 array('name' => 'members','onclick' => 'test12'),
 array('name' => 'members','onclick' => 'test13'),
 array('name' => 'members','onclick' => 'test21'),
 array('name' => 'members','onclick' => 'test22'),
 array('name' => 'members','onclick' => 'test23'),
 array('name' => 'members','onclick' => 'test34'),
 array('name' => 'members','onclick' => 'test35')
);

navigation_permission(3) would return everything as from the previous example, except the two arrays containing test34 and test35, cause 5 is higher than 3.

navigation_permission(2) would return the same in 1 and two. navigation_permission(1) would return only the arrays in 1.

example two:

navigation_permission(3)

 array(
        array('name' => 'members','onclick' => 'test11'),
     array('name' => 'members','onclick' => 'test12'),
     array('name' => 'members','onclick' => 'test13'),
     array('name' => 'members','onclick' => 'test21'),
     array('name' => 'members','onclick' => 'test22'),
     array('name' => 'members','onclick' => 'test23'),
    );

Q: How can i make the navigation_permission function return this kind?

maria
  • 207
  • 5
  • 22
  • 56
  • 1
    Hello! Did you even tried to research [PHP manuals](http://php.net/manual/en/ref.array.php)? There are lots of array functions there, to do any kind of stuff you want, with whole, easy-to-get and well commented examples. There is searching, sorting, shuffling, splitting, pushing, popping, dropping, flying, folding, bursting into flames, just give it some read, try something! If you have a problem **after** trying, post your attempt here and the problem, and we'll help! – al'ein Oct 07 '15 at 13:27
  • Could you spoonfeed what function to look after that would help me achieve this? @AlanMachado – maria Oct 07 '15 at 13:29
  • **Before that**, I have to point that your question seems like the [XY Problem](http://xyproblem.info/). What are you *really* trying to do with this function? Maybe there is a better way to achieve what you want. – al'ein Oct 07 '15 at 13:32

2 Answers2

0

this will return all the arrays which are less than $permission

 function navigation_permission($permission) {
            $initialArray = array(
            1 => array(
                array('name' => 'members','onclick' => 'test11'),
                array('name' => 'members','onclick' => 'test12'),
                array('name' => 'members','onclick' => 'test13').
                ),
            2 => array(
            array('name' => 'members','onclick' => 'test21'),
            array('name' => 'members','onclick' => 'test22'),
            array('name' => 'members','onclick' => 'test23'),
            ),

            5 => array(
            array('name' => 'members','onclick' => 'test34'),
            array('name' => 'members','onclick' => 'test35'),
            )
            );
            $returnArr = array();
            for($i=1; $i <= $permission; $i++){
               foreach($initialArray[$i] as $row){

                $returnArr[] = $row;
               }
             }
            return $returnArr;
        }
-1

You can use something like this function.

function navigation_permission($permission)
{

    $res = [];

    switch ($permission)
    {
        case 6:
            $res = array_merge($res,[
                [ 'name' => 'members','onclick' => 'test11' ],
                [ 'name' => 'members','onclick' => 'test12' ],
                [ 'name' => 'members','onclick' => 'test13' ],
            ]);

        case 5:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);

        case 4:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);

        case 3:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);

        case 2:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);

        case 1:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);
        break;
    }

    return $res;

};

Yes, break statement in 5 of 6 cases is absent to provide functionality you need:
Switch control structure: Manual on php.net
Discussion on stackoverflow: switch statement without break

Community
  • 1
  • 1
  • Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – ryanyuyu Oct 08 '15 at 15:12