Question

How can I merge two different multidimensional arrays but copy the keys and values from the first array to the second array?

How I'm trying to get it to work is; the first array sets the input fields, which are then cloneable (cloning using jQuery, exactly like this: http://jsfiddle.net/8M98y/). Once you save field data, the second array is the resulting output. The problem is, the second array lacks the specific keys and values from the first array that are needed to output the saved data correctly.

Is there was a way to join or merge the arrays while copying the keys and values from the first array into the second array?

The first array that I need to copy the keys/values from looks like this:

Array
(
    [group-1] => Array
        (
            [fields] => Array
                (
                    [text-field-1] => Array
                        (
                            [name] => Text Field 1
                            [value] => Value 1
                            [comments] => true
                        )

                    [text-field-2] => Array
                        (
                            [name] => Text Field 2
                            [value] =>  Value 2
                            [comments] => false
                        )

                )

        )

)

The second array looks like this:

Array
(
    [group-1] => Array
        (
            [fields] => Array
                (
                    [text-field-1] => New value here
                    [text-field-2] => New value also
                )

        )

    [group-2] => Array
        (
            [fields] => Array
                (
                    [text-field-1] => Cloned group with new value
                    [text-field-2] => Cloned group with new value also
                )

        )

)

So if these two arrays were able to be merged, would need the output of the merged array to look like this: http://pastebin.com/uzuZs73B

I've tried using just array_merge( $array2, $array1 ), but the resulting output looks like this: http://pastebin.com/DucKGMN3 where they are in fact merged, but the keys and values aren't copied over.

EDIT: Should describe a use case here.

So how this works, the initial, unsaved output is two text inputs in a group which is created by the first array. The group is cloneable, using jQuery clone (this example here: http://jsfiddle.net/8M98y/). So if you add/clone one of more groups and then save, the resulting saved data would be the second array. The strings in the second array is the actual saved input values. Those would go into [value] in the first array.

However, the output of the fields is still based on the first array, meaning it can't output the cloned groups correctly as they're not an array and don't have the same keys and values from the first array.

enter image description here

If anyone can provide some insight on this, it would be hugely and greatly appreciated.

Was it helpful?

Solution

Sorry if I misunderstood the question, but is merging a requirement? If you have access to both arrays you could iterate over the second array, mapping the original key values, and overwriting with the new values as you go.

Your arrays:

$arr1 = Array
(
    'group-1' => Array
        (
            'fields' => Array
                (
                    'text-field-1' => Array
                        (
                            'name' => 'Text Field 1',
                            'value' => 'Value 1',
                            'comments' => 'true'
                        ),

                    'text-field-2' => Array
                        (
                            'name' => 'Text Field 2',
                            'value' =>  'Value 2',
                            'comments' => 'false'
                        )

                )

        )

);


$arr2 = Array
(
    'group-1' => Array
        (
            'fields' => Array
                (
                    'text-field-1' => 'New value here',
                    'text-field-2' => 'New value also'
                )

        ),

    'group-2' => Array
        (
            'fields' => Array
                (
                    'text-field-1' => 'Cloned group with new value',
                    'text-field-2' => 'Cloned group with new value also'
                )

        )

);

Secret sauce:

foreach($arr2 as $k=>$v){
    // Get the new values for this iteration
    $val1 = $arr2[$k]['fields']['text-field-1'];
    $val2 = $arr2[$k]['fields']['text-field-2'];
    // Duplicate the original array
    $arr2[$k]['fields'] = $arr1['group-1']['fields']; 
    // Insert the new values
    $arr2[$k]['fields']['text-field-1']['value'] = $val1;
    $arr2[$k]['fields']['text-field-2']['value'] = $val2;
}


echo '<pre>';
print_r($arr2);
echo '</pre>';

exit();

Which returns:

Array
(
    [group-1] => Array
        (
            [fields] => Array
                (
                    [text-field-1] => Array
                        (
                            [name] => Text Field 1
                            [value] => New value here
                            [comments] => true
                        )

                    [text-field-2] => Array
                        (
                            [name] => Text Field 2
                            [value] => New value also
                            [comments] => false
                        )

                )

        )

    [group-2] => Array
        (
            [fields] => Array
                (
                    [text-field-1] => Array
                        (
                            [name] => Text Field 1
                            [value] => Cloned group with new value
                            [comments] => true
                        )

                    [text-field-2] => Array
                        (
                            [name] => Text Field 2
                            [value] => Cloned group with new value also
                            [comments] => false
                        )

                )

        )

)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top