I have many arrays containing an ID as the primary key, with multidimensional information under each id. Here are two examples:
First Example Array:
array
14181 =>
array
'industries' =>
array
'weight' => string '105652' (length=6)
'count' => string '11' (length=2)
48354 =>
array
'industries' =>
array
'weight' => string '508866' (length=6)
'count' => string '10' (length=2)
Second Example Array:
array
16434 =>
array
'business_types' =>
array
'weight' => string '104614' (length=6)
'count' => string '1' (length=1)
48354 =>
array
'business_types' =>
array
'weight' => string '103610' (length=6)
'count' => string '10' (length=2)
I'd like to get the intersection of many arrays like these ( based on the key ), but I need to preserve the weight and count data from each array for each key. Notice it's different weight and count data from each array. In this case, business_type and industries.
Final Array Needed:
array
48354 =>
array
'business_types' =>
array
'weight' => string '103610' (length=6)
'count' => string '10' (length=2)
'industries' =>
array
'weight' => string '508866' (length=6)
'count' => string '10' (length=2)
Originally I was not trying to keep the weights and counts, so I was simply performing an array_intersect_keys()
and the job was done. Now I need to keep this data. I named the sub arrays different things in the hope that array_intersect_keys()
would preserve it, however, it only preserves it for the first array in the function.
Is there a preferred way to do something like this?
The only solution I can come up with is to reduce all the arrays to a list of final ID's ( keys ) and then loop through that array pulling the weight and count info from each of the original arrays we compared.