3

I am struggling with a data structure in PHP. I'm trying to use array_merge_recursive to condense an array by like keys and then grab all values instead of having them overwritten. This is why I chose array_merge_recursive instead of array_merge.

My array is something similar to:

Array
(
  [0] => Array
  (
    [App] => APP1
    [Type] => DB
  )
  [1] => Array
  (
    [App] => APP1
    [Type] => WEBSITE
  )
  [2] => Array
  (
    [App] => APP2
    [Type] => IOS
  )
)

I was expecting array_merge_recursive to combine like keys and then group the other elements into arrays however this is not the behavior I am seeing.

I am hoping to get an array like the following:

Array
(
  [0] => Array
  (
    [App] => APP1
    [Type] => Array
    (
      [0] => DB
      [1] => WEBSITE
    )
  )
  [1] => Array
  (
    [App] => APP2
    [Type] => IOS
  )
)
2
  • not the behavior I am seeing in your result you move DB from key Type to an subarray with key 0. Same for WEBSITE. How do you thing array_merge_recursive works? And try to explain the logic behind your expected result. Else noone can help. Commented Dec 15, 2016 at 15:34
  • I was under the impression that array_merge_recursive could work with one array. I've assumed incorrectly. I'm just trying to combine "APP" keys together and preserve the value assigned to the "TYPE" key. Commented Dec 19, 2016 at 14:25

1 Answer 1

2

array_merge_recursive() does not do what you think it does. You are looking for a function that restructures an array based on specific rules that are helpful to you and as such there isn't a builtin php function for that. I.e. How would PHP know that you wanted to new array to be structured by APP rather TYPE. Assuming your array is always that simple, the easiest version of the function you want looks something like this:

function sortByApp($array){
    $result = array();
    foreach($array as $row){
        if(!isset( $result[ $row['APP'] ] ) {
            $result[ $row['APP'] ] = array(
                'APP' => $row['APP'],
                'TYPE' => array( $row['TYPE'] ) 
            );
        }else{
            $result[ $row['APP'] ]['TYPE'] = array_merge( $result[ $row['APP'] ]['TYPE'], array($row['TYPE']) );
        }
    }
    $result =  array_values($result); //All this does is remove the keys from the top array, it isn't really necessary but will make the output more closely match what you posted.

    return $result

}

Note, in this solution, the value of the TYPE key in every APP will always be an array. This makes handling the data later easier in my opinion since you don't have to worry about checking for a string vs an array.

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

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.