duan19750503 2016-02-17 07:07
浏览 57
已采纳

使用来自1个数组的键和来自另一个数组的值合并2个PHP数组,但如果该键不存在,则必须删除

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
)
*/
  • 写回答

2条回答 默认 最新

  • dsdsds12222 2016-02-17 07:17
    关注

    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
    )
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?