Changing Key Values in Array

In an array something like the following, I need to replace the keys with names from another array. Unfortunately I am too long-retired and was never well-versed in arrays anyway so I can’t seem to figure it out!

This example has only two sub-arrays but most have more although the layers of nesting remains the same and the number of elements in each sub-array is always the same too. This array was generated elsewhere in the script from a flat array into these chunks.

$SplitFields = Array
(
    [0] => Array
        (
            [0] => Login
            [1] => UserLogin
            [2] => 
            [3] => 1
            [4] => 0
            [5] => 25
            [6] => 1
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 0
            [12] => 1
            [13] => 1
        )

    [1] => Array
        (
            [0] => Password
            [1] => UserPassword
            [2] => 
            [3] => 18
            [4] => 0
            [5] => 25
            [6] => 1
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 0
            [12] => 2
            [13] => 1
        )
)

This is the $Newkeys array with the key values.

$NewKeys = [
		'FieldLabel',
		'FieldTitle',
		'FieldValue',
		'FieldType',
		'FieldHeight',
		'FieldLength',
		'FieldRequired',
		'FieldNote',
		'LookupQuery',
		'Multiple',
		'LookupDB',
		'AutoSubmit',
		'ViewOrder',
		'ShowField'
		];

I tried too many things to post here, some involving loops and some not but I think I need something like this although it need not be a function as coding directly into the script is fine too:

function array_keys_multi($array) {
		$NewKeys = [];
		foreach ($array as $key => $value) :
			$NewKeys[] = $key;
			
			showValues($value);
			if (is_array($array[$key])) :
				$FormFields = array_merge($NewKeys, array_keys_multi($array[$key]));
			endif;
		endforeach;
	return $FormFields;
	} 

The function you are looking for is array_combine(), though you should be applying it when you are retrieving this data.

foreach($SplitFields as $key=>$arr)
{
	$SplitFields[$key] = array_combine($NewKeys,$arr);
}

So simple and works perfectly! I was trying to make it more complicated than it actually needed to be so thank you for sorting it out for me.

Sponsor our Newsletter | Privacy Policy | Terms of Service