I am tring to take 2 PHP arrays which hold PLugin data.
1 array has plugin key/values from database and the other is from plugin file headers
I need to create a new array that merges the values from the file array into the db array.
If the db array has a key that does not exist in the file array, that key should be removed from the final array.
If the file array has a key that does not exist in the db array, it should remain in the file array.
The final array output goal is to make sure the new array always has all the keys that exist in the DB array and those keys have values that are from matching keys in the file array.
New Array:
keys = from DB array. If the File array is missing a key from the DB array, remove it from the new array.
values = from the File array where the DB array key matches the array key in File array, then return that value for the new array.
I have tested using PHP's array_merge
, array_replace
, and array_intersect_keys
. There code and output are shown below...
array_intersect_keys
get the closest to my desired result but it does not remove keys that exist in DB array and are not present in the file array.
// array_merge()
$dbPluginArray = array(
'version' => '',
'name' => 'db-name',
'db-key-not-exist-in-file-array' => '' // this key should be removed from the final array
);
$filePluginArray = array(
'version' => 'file-version',
'name' => 'file-name',
'file-key-not-exist-in-db-array' => 'trapezoid'
);
$newMergeArray = array_merge($dbPluginArray, $filePluginArray);
echo '<pre>';
print_r($newMergeArray );
echo '</pre>';
/*
Array
(
[version] => file-version
[name] => file-name
[db-key-not-exist-in-file-array] =>
[file-key-not-exist-in-db-array] => trapezoid
)
*/
// array_replace()
$dbPluginArray = array(
'version' => '',
'name' => 'db-name',
'db-key-not-exist-in-file-array' => '' // this key should be removed from the final array
);
$filePluginArray = array(
'version' => 'file-version',
'name' => 'file-name',
'file-key-not-exist-in-db-array' => 'trapezoid'
);
$newReplaceArray = array_replace($dbPluginArray, $filePluginArray);
echo '<pre>';
print_r($newReplaceArray);
echo '</pre>';
/*
Array
(
[version] => file-version
[name] => file-name
[db-key-not-exist-in-file-array] =>
[file-key-not-exist-in-db-array] => trapezoid
)
*/
// array_intersect_key
$dbPluginArray = array(
'version' => '',
'name' => 'db-name',
'db-key-not-exist-in-file-array' => '', // this key should be removed from the final array,
'test' => 'testval'
);
$filePluginArray = array(
'version' => 'file-version',
'name' => 'file-name',
'file-key-not-exist-in-db-array' => 'trapezoid',
'test' => 'testval'
);
echo '<pre>';
$result = array_intersect_key($filePluginArray,$dbPluginArray);
print_r($result);
echo '</pre>';
/*
Array
(
[version] => file-version
[name] => file-name
[test] => testval
)
*/
What about
$dbPluginArray = array(
'version' => '',
'name' => 'db-name',
'db-key-not-exist-in-file-array' => '' // this key should be removed from the final array
);
$filePluginArray = array(
'version' => 'file-version',
'name' => 'file-name',
'file-key-not-exist-in-db-array' => 'trapezoid'
);
echo '<pre>';
$result = array_merge(array_intersect_key($filePluginArray,$dbPluginArray), $filePluginArray);
print_r($result);
echo '</pre>';
/*
Array
(
[version] => file-version
[name] => file-name
[file-key-not-exist-in-db-array] => trapezoid
)
*/
$dbPluginArray = array(
'version' => '',
'name' => 'db-name',
'db-key-not-exist-in-file-array' => '', // this key should be removed from the final array,
'test' => 'testval'
);
$filePluginArray = array(
'version' => 'file-version',
'name' => 'file-name',
'file-key-not-exist-in-db-array' => 'trapezoid',
'test' => 'testval'
);
echo '<pre>';
$result = array_merge(array_intersect_key($filePluginArray,$dbPluginArray), $filePluginArray);
print_r($result);
echo '</pre>';
/*
Array
(
[version] => file-version
[name] => file-name
[test] => testval
[file-key-not-exist-in-db-array] => trapezoid
)
*/