In PHP, given the following arrays, how can I get them together
$ Masterkeys = array ('key1', 'key2'); $ Settings_foo = array (array ('id' = & gt; 'key1', 'fu certification' = & gt; 'some value')); $ Settings_bar = Array (array ('id' = & gt; 'key1', 'bar-certification' = & gt; 'some other value'));
Finally, $ masterkeys to each settings_ [foo | Bar] is necessary for combination of arrays
array (['key1'] = array ('foo certification' = & gt; 'some value', 'bar-setting' = & Gt; 'some other value'));
I know that I can use a pair of foreach loops on it, but just thinking that there are some PHP array functions that can add them together
You can use some of PHP's array functions, but your input data is not in very good form . You will have the lowest iterative (and perhaps the best performance) by typing yourself:
# $ masterkey = & gt; Array () $ result = array_combine ($ masterkeys, array_fill (0, count ($ masterkies), array ())); $ Settings $ settings as foreach ($ settings as $ foreach ($ s) via foreach (array ($ settings_foo, $ settings_bar), array settings) # # Merge the array only if ID is in the master list $ Key = $ s ['id']; If (array_key_exists ($ key, $ result)) $ result [$ key] = array_merge ($ result [$ key], $ s); }} # Do not set all external 'id' properties as foregoing (array_keys ($ result) $ i) ($ result [$ i] ['id']); Var_dump ($ result);
As an alternative, you can see in the array_map
and array_filter
, but the way the data is structured, I'm not sure That they will be very used.
Comments
Post a Comment