0

I have an multi dimensional array i am trying to group the array based on the key value.

So, I'm trying to group them by key but i am not getting to group the array based on the key values.

Below is the original array

 Array
    (
        [0] => Array
            (
                [User] => Array
                    (
                        [id] => 2
                        [feature] => AddUser
                        [feature_level] => 1
                        [parent_feature] => 1
                    )

            )

        [1] => Array
            (
                [User] => Array
                    (
                        [id] => 3
                        [feature] => EditUser
                        [feature_level] => 1
                        [parent_feature] => 1
                    )

            )

        [2] => Array
            (
                [Candidate] => Array
                    (
                        [id] => 5
                        [feature] => AddCandidate
                        [feature_level] => 1
                        [parent_feature] => 4
                    )

            )

        [3] => Array
            (
                [Candidate] => Array
                    (
                        [id] => 6
                        [feature] => EditCandidate
                        [feature_level] => 1
                        [parent_feature] => 4
                    )

            )

    )

What i need is

   Array (

   [User] => Array (
          [0] => Array (
                    [id] => 2
                    [feature] => AddUser
                    [feature_level] => 1
                    [parent_feature] => 1
                    )
          [1] => Array (
                    [id] => 3
                    [feature] => EditUser
                    [feature_level] => 1
                    [parent_feature] => 1
                    )
          )

   [Candidate] => Array (
          [0] => Array (
                    [id] => 5
                    [feature] => AddCandidate
                    [feature_level] => 1
                    [parent_feature] => 4
                 )

          [1] => Array (
                    [id] => 5
                    [feature] => EditCandidate
                    [feature_level] => 1
                    [parent_feature] => 4
                 )
          )

)

2 Answers 2

2

Here is a solution with demo. Might help you:

// $myArr is your origional array.
$result_arr = [];

array_walk($myArr,function($v,$k) use (&$result_arr){
   $result_arr[key($v)][] = $v[key($v)]; 
});

print_r($result_arr);

Click Here for Demo

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

Comments

1

If your initial array is called $source, this should do it:

$result =[]; //final array
foreach($source as $data){
    $type = key($data); //eg: 'Candidate' or 'User'
    if(!isset($result[$type])) $result[$type]=[];
    $result[$type][] = reset($data);
}

Demo here

4 Comments

User and Candidate values fetched from the db is dynamic it is like parent feature and add,edit are like child feature need to map w.r.t parent and child
You mean there can be more user-types thans 'User' and 'Candidate' ? Meaning you could also have 'Admin', or 'Mods'...
yes and it may also have like User->adduser,User->viewUser->viewResume. Wt to do in this case where Parent is User its child is View User and view users child is viewresume. What to do in this case
Cool. I rewrote my solution to remove hard-coded strings.

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.