2017-08-02 153 views
0

我有2個數組,一個帶有鍵,另一個帶有數字鍵,我如何複製它們的鍵,以正確的順序替換數字鍵?將陣列密鑰複製到另一個現有陣列密鑰

陣列與數字鍵

Array 
(
    [0] => ABCDEFG 
    [1] => This is my description 
    [2] => 12.00 
    [3] => 30.00 
    [4] => My supplier 
    [5] => My brand 
    [6] => Shoes 
    [7] => 

) 

陣列2

Array 
(
    [productcode] => Product Code 
    [productitemdesc] => Description 
    [retailsalesprice] => Selling Price 
    [currentcost] => Unit Cost 
    [supplier] => Supplier 
    [productbrand] => Brand 
    [productcategory] => Category 
    [productgroup] => Group 
) 

我希望這樣的事情

Array 
    (
     [productcode] => ABCDEFG 
     [productitemdesc] => This is my description 
     [retailsalesprice] => 12.00 
     [currentcost] => 30.00 
     [supplier] => My Supplier 
     [productbrand] => My Brand 
     [productcategory] => Shoes 
     [productgroup] => 
    ) 

是否有任何現有的功能,可用於PHP?試過array_fill_keys,但似乎並不是我想要的。

+0

什麼是反對票? LOL –

回答

5

可以使用功能array_combine()將密鑰從所述第二陣列結合(對於下面的例子中被稱爲$array_keys)與來自第一數組中的值(稱爲$array_values):

實施例:

$combined = array_combine(array_keys($array_keys), $array_values); 
print_r($combined); 

這將完全按照您所描述的方式打印出陣列。

0

array_combine方式是好多了,但你也可以使用這個功能。這將允許您根據需要修改這些值。

function custom_combine($numeric_array, $keyed_array) 
{ 
    $temp = array(); 
    $i=0; 
    foreach($keyed_array as $key=>$val) 
    { 
     if(isset($numeric_array[$i]))  
      $temp[$key] = $numeric_array[$i]; 
     else 
      $temp[$key] =''; 
     $i++;   
    } 
    return($temp); 
} 
0

其他的答案肯定是更有效的,但如果你想了解如何手動遍歷數組,這樣的事情應該工作:

<?php 

// The original array 
$arr1 = array(
    0 => 'ABCDEFG', 
    1 => 'This is my description', 
    2 => '12.00', 
    3 => '30.00', 
    4 => 'My supplier', 
    5 => 'My brand', 
    6 => 'Shoes', 
    7 => '', 
); 

// The second array 
$arr2 = array(
    'productcode' => 'Product Code', 
    'productitemdesc' => 'Description', 
    'retailsalesprice' => 'Selling Price', 
    'currentcost' => 'Unit Cost', 
    'supplier' => 'Supplier', 
    'productbrand' => 'Brand', 
    'productcategory' => 'Category', 
    'productgroup' => 'Group', 
); 

// Pre-define the new array to avoid errors 
$arr_new = array(); 

// Manually create a value to increment during our foreach loop 
$increment = 0; 

// Loop through each value in $arr2 
foreach ($arr2 as $key2 => $value2) { 
    // If the key is set in $arr1, assign the value from $arr1 and the key from $arr2 
    // to the new array 
    if (isset($arr1[$increment])) { 
    $arr_new[$key2] = $arr1[$increment]; 
    } 
    // Increment the value regardless of whether it was found in $arr1 or not 
    $increment++; 
} 

// Remove this if you want... It just displays the values found in $arr_new 
print_r($arr_new); 
0

我測試了它和工作:

<?php 
$a=array(
0 => "ABCDEFG", 
1 => "This is my description", 
2 => "12.00", 
3 => '30.00', 
4 => 'My supplier', 
5 => 'My brand', 
6 => 'Shoes', 
7 => '', 

) 
; 
$b=array 
(
'productcode' => 'Product Code', 
'productitemdesc' => 'Description', 
'retailsalesprice' => 'Selling Price', 
'currentcost' => 'Unit Cost', 
'supplier' => 'Supplier', 
'productbrand' => 'Brand', 
'productcategory' => 'Category', 
'productgroup' => 'Group', 
); 
$j=0; 
foreach ($b as $i => $value) { 
    $b[$i]=$a[$j]; 
    $j++; 
} 
var_dump($b); 

?>

-1

你可以使用array_combine()

$array3=array_combine($array2,$array1); 
    print_r($array3); 
+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/16904391) – Milap