如何将一个键数组与另一个数组中的相应值组合起来?

发布于 2024-09-14 18:37:36 字数 603 浏览 3 评论 0原文

我有2个数组

Array ( [1] => Manufacturer [2] => Location [3] => Hours [4] => Model ) 

Array ( [Manufacturer] => John Deere [Location] => NSW [Hours] => 6320 ) 

需要将它们组合起来并将第一个数组(制造商、位置、小时、型号)中的值关联为第二个数组中的名称,并且如果第一个数组中的特定值在第二个数组中找不到要关联的关联名称空的 。对于示例,我需要从上面的数组得到的结果是这样的数组

   Array ( [Manufacturer] => John Deere [Location] => NSW [Hours] => 6320 [Model] => ) 

如果我使用简单的 array_combine 它会说 PHP警告:array_combine()[function.array-combine]:两个参数应该具有相同数量的元素

I have 2 arrays

Array ( [1] => Manufacturer [2] => Location [3] => Hours [4] => Model ) 

and

Array ( [Manufacturer] => John Deere [Location] => NSW [Hours] => 6320 ) 

I need to combine them and associate the values from the first array( Manufacturer, Location, hours , Model) as names in the 2nd array and if a specific values from the first array doesn't find associative name in the 2nd array to associate empty . For the example the result that I need from the above arrays is an array like this

   Array ( [Manufacturer] => John Deere [Location] => NSW [Hours] => 6320 [Model] => ) 

If i use simple array_combine it says that PHP Warning: array_combine() [function.array-combine]: Both parameters should have equal number of elements

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何以畏孤独 2024-09-21 18:37:36

您可以使用简单的 foreach 循环:

$combined = array();
foreach ($keys as $key) {
    $combined[$key] = isset($values[$key]) ? $values[$key] : null;
}

其中 $keys 是包含键的第一个数组,$values 是包含相应值的第二个数组。如果$values中没有对应的值,则该值为null

You can use a simple foreach loop:

$combined = array();
foreach ($keys as $key) {
    $combined[$key] = isset($values[$key]) ? $values[$key] : null;
}

Where $keys is your first array with the keys and $values is your second array with the corresponding values. If there is no corresponding value in $values the value will be null.

风透绣罗衣 2024-09-21 18:37:36

假设 $array1 和 $array2 按照您列出的顺序排列,我认为这会起作用:

$newArray = array();
while (list(,$data) = each($array1)) {
  if (isset($array2[$data])) {
     $newArray[$data] = $array2[$data];
  } else {
     $newArray[$data] = "";
  }
}

Assuming $array1 and $array2 in order as you listed them, I think this would work:

$newArray = array();
while (list(,$data) = each($array1)) {
  if (isset($array2[$data])) {
     $newArray[$data] = $array2[$data];
  } else {
     $newArray[$data] = "";
  }
}
2024-09-21 18:37:36

尝试 array_merge()。这个例子似乎做了你想做的事:

<?php
$keys = array( 1 => "Manufacturer", 2 => "Location", 3 => "Hours", 4 => "Model") ;
$canonical = array_combine(array_values($keys), array_fill(0, count($keys), null));

$data = array("Manufacturer" => "John Deere", "Location" => "NSW", "Hours" => 6320);

print_r(array_merge($canonical, $data));

Try array_merge(). This example appears to do what you want:

<?php
$keys = array( 1 => "Manufacturer", 2 => "Location", 3 => "Hours", 4 => "Model") ;
$canonical = array_combine(array_values($keys), array_fill(0, count($keys), null));

$data = array("Manufacturer" => "John Deere", "Location" => "NSW", "Hours" => 6320);

print_r(array_merge($canonical, $data));
七颜 2024-09-21 18:37:36

如果键数组是 $array1,并且具有值的关联数组是 $array2:

$new_array = array_merge(array_fill_keys(array_flip($array1), ''), $array2));

这会反转您的键数组,并用 '' 值填充它。然后,当您合并数组时,任何重复的键都将被第二个数组覆盖,但未填充的键将保留。

If the array of keys is $array1, and the associative array with values is $array2:

$new_array = array_merge(array_fill_keys(array_flip($array1), ''), $array2));

This inverts your key array, filling it with '' values. Then when you merge the arrays, any duplicate keys will be overwritten by the second array, but unfilled keys will remain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文