dongqie5529 2017-01-24 09:54
浏览 60
已采纳

基于关系数组合并2个数组

I have the following arrays:

$excel_arr = array(
         ["C1", "Title 3"],
         ["A1", "Title 1"],
         ["B1", "Title 2"],
         ["D1", "Title 4"]
);
$db_result = array(
         "title_2" => "Cell 2 Value",
         "title_1" => "Cell 1 Value",
         "title_3" => "Cell 3 Value",
         "title_5" => "Cell 5 Value"
);
$excel_db_relation = array(
         "title_1" => "Title 1",
         "title_2" => "Title 2",
         "title_3" => "Title 3",
         "title_4" => "Title 4",
         "title_5" => "Title 5"
);

usort($excel_arr, function ($a, $b) { return strnatcmp($a[0], $b[0]); });
  • $excel_arr is an array with the titles for each column in an excel file. The first cell defines the cell coordinate and the second the actual cell value.

  • $db_result is an array containing queried values from a database. The key is column name in the table.

  • $excel_db_relation is an array which defines the relation between the 2 former arrays. Which excel column is linked to which db table column. In this example they are very similar, but in practice there might be more than just an underscore that differs.

The cell coordinates in $excel_arr defines the order in which each value must be printed. To do this I sort the array with the usort() as seen above.


I need to somehow merge these arrays so that the resulting array becomes:

array("Title 1" => "Cell 1 Value", "Title 2" => "Cell 2 Value", "Title 3" => "Cell 3 Value")

The database array didn't return a value for cell 4 and the excel sheet doesn't define a E5 cell. So these must not be included in the resulting array.


I have tried array_merge($excel_db_relation, $db_result) and various combinations of array_merge() and array_flip() but no matter what I do I can't seem to merge the arrays with "Title X" being the key.

  • 写回答

3条回答 默认 最新

  • dongtan9465 2017-01-24 10:19
    关注

    The solution using array_intersect_key, array_intersect and array_column functions:

    $result = [];
     // getting concurrent 'titles'(by key)
    $titles = array_intersect_key($excel_db_relation, $db_result); 
    
    foreach (array_intersect($titles, array_column($excel_arr, 1)) as $k => $v) {
        $result[$v] = $db_result[$k];
    }
    
    print_r($result);
    

    The output:

    Array
    (
        [Title 1] => Cell 1 Value
        [Title 2] => Cell 2 Value
        [Title 3] => Cell 3 Value
    )
    

    Update:
    Alternative approach to hold the order in which each value must be printed.
    Used functions: array_merge_recursive(to combine cell titles and values into separate groups) and array_column functions:

    $result = [];
    $bindings = array_column(array_merge_recursive($db_result, $excel_db_relation), 0, 1);
    foreach (array_column($excel_arr, 1) as $title) {
        if (isset($bindings[$title])) $result[$title] = $bindings[$title];
    }
    
    print_r($result);
    

    The output:

    Array
    (
        [Title 3] => Cell 3 Value
        [Title 1] => Cell 1 Value
        [Title 2] => Cell 2 Value
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行