What's the best way to merge to arrays based on key values in each array?

zeropsi

I have these two arrays that I am currently using two loops to merge. I was wondering if there was a better way to merge these arrays:

$args = array(
  'Name' => 'Test Name', 
  'Email' => '[email protected]', 
  'Message' => 'There is no place like 127.0.0.1!'
);

$fields = array(
  2 => array(
    'label' => 'Name',
    'id' => 2
  ),
  3 => array(
    'label' => 'Email',
    'id' => 3
  ),
  4 => array(
    'label' => 'Message',
    'id' => 4
  )
);

I am trying to merge them into an array like this:

$merged = array(
  2 => array(
    'label' => 'Name',
    'id' => 2,
    'value' => 'Test Name'
  ),
  3 => array(
    'label' => 'Email',
    'id' => 3,
    'value' => '[email protected]'
  ),
  4 => array(
    'label' => 'Message',
    'id' => 4,
    'value' => 'There is no place like 127.0.0.1!'
  )
);

I am currently using this dual foreach loop to do it:

foreach ( $args['fields'] as $k => $v ) {
        foreach( $fields['fields'] as $i => $item ) {
            if ( $item['label'] === $k ) {
                $fields['fields'][$i]['value'] = $v;
            }
        }
    }

I have tried using array_merge and array_merge_recurisve, but it throws off the array keys from the $fields array.

Any thoughts or better approaches to try and achieve my desired outcome?

Barmar

You don't need nested loops. Just loop over the $fields array, and then access the corresponding entry in $args with an index.

$merged = []
foreach ($fields as $key => &field) {
    $field['value'] = $args[$field['label']];
    $merged[$key] = $field;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the best way to "merge" 2 arrays by each value?

What's the best way to document an array of arrays of arrays using JSDoc?

What is the best way to merge 2 byte arrays?

What is the best way to merge nested arrays

Best way to merge objects using key in an array?

Merge arrays in multidimensional array based on multiple values

Merge objects in an array based on key values

Best way to merge two Arrays of Classes based on Class variable value

Best way Merge two string arrays based on a condition

What is the best way to compare 2 arrays and echo out the values of the same content of the array in php?

Best way to merge two dataframes based on the key column

Best way to merge two maps and sum the values of same key?

Python: What is the best way to determine the language of a text based on each letter's frequency?

What's the most efficient way to merge chained git branches that are based on each other into master?

What's the best way to ensure no null values

What is the best way to access key/values of a object array when I don't know them?

What's the best way to split an array into multiple arrays every time an element is met?

Merge two arrays, one array each 5 key

In Swift, what is the best way to assign two arrays to key/value of a dictionary?

Best way to clear a PHP array's values

What's the best way to get top N elements in a Map<String, Double> based on their values?

What's the best way to downsample a numpy array?

ruby array of hash merge array values based on key

What the best way to deal with codeigniter php array_merge, in a foreach?

What's the best way to set all values of a three-dimensional array to zero in Java?

What's the best way (ES6 allowed) to extract values from an array and convert them to a string?

What's the best way to merge to the same table several times in Postgresql?

What is a best way to intersect multiple arrays with numpy array?

What is the best way to merge hashes?