我有一個數據庫查詢的數組:轉換鍵 - 值對數組關聯數組,而不使用列名
Array (
[0] => Array ([div_id] => 21 [div_name] => "Alphabet")
[1] => Array ([div_id] => 16 [div_name] => "Elementary")
[2] => Array ([div_id] => 19 [div_name] => "Preschool")
[3] => Array ([div_id] => 20 [div_name] => "Secondary")
)
我想將其改造成:
Array (
[21] => "Alphabet"
[16] => "Elementary"
[19] => "Preschool"
[20] => "Secondary"
)
然而, 我無法使用密鑰名稱。我需要引用它們作爲第一列和第二列,因爲在我的上下文中,我無法控制源數據的鍵。我需要使用相同的算法對不同的鍵名的數據,例如:
Array (
[0] => Array ([unknown_id] => 21 [random] => "Alphabet")
[1] => Array ([unknown_id] => 16 [random] => "Elementary")
[2] => Array ([unknown_id] => 19 [random] => "Preschool")
[3] => Array ([unknown_id] => 20 [random] => "Secondary")
)
的問題是非常相似Convert array of key-value pairs into associative array但有[0]和[1]鍵,和我有文字鍵但仍然需要引用數組的第一項和第二項。
最差來最糟糕的是,只需遍歷數組並手動完成即可。 – Daniel