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_arris 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_resultis an array containing queried values from a database. The key is column name in the table.$excel_db_relationis 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.
$excel_arrdefines the order, why sort it? That said, could you add an example of the final array?$excel_arrthat defines the order. It needs to beA1, B1, C1, D1. They are not sorted above. ~ Which final array do you mean? The final result is already in my post at the bottom.