2017-08-02 116 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