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条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示