[英]How to combine two arrays together?
有沒有一種快速的方法來將一個數組的值組合為另一個數組的鍵?
輸入:
array A => Array (
[0] => "cat"
[1] => "bat"
[2] => "hat"
[3] => "mat"
)
array B => Array (
[0] => "fur"
[1] => "ball"
[2] => "clothes"
[3] => "home"
)
預期產量:
array C => Array (
[cat] => "fur"
[bat] => "ball"
[hat] => "clothes"
[mat] => "home"
)
我該怎么辦?
array_combine()
會完全滿足您的要求。
引用手冊:
array array_combine ( array $keys , array $values )
通過將keys數組中的值用作鍵,並將values數組中的值用作對應值來創建數組。
對於您的情況,您必須執行以下操作:
$array['C'] = array_combine($array['A'], $array['B']);
當然,您也可以使用循環的各種組合來執行此操作,但array_combine()
可能是最簡單的解決方案。
您可以使用array_combine
輕松做到這array_combine
:
// First parameter will be used as the keys, the second for the values
$new_array = array_combine($keys_array, $values_array);
試試這個: array_combine($a, $b);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.