如何使用重复键合并两个数组

时间:2017-10-02 20:03:32

标签: php arrays

我有两个数组$ rate_row和$ totalrowcost我试图根据第一个数组中的键更新第二个数组$ totalrowcost中的一行$ rate_row两个数组的长度总是相同。

我使用array_combine函数

组合两个数组

如果数组中没有重复的键,但是如果我在有重复的键时将它们组合在一起,则下面的代码可以正常工作

我没有得到理想的结果。

实施例: 如果$ rate_row中的键为0,我需要将$ totalrowcost中的值设置为0,但在组合数组时,其中一行将被删除,只保留一行键为0的行。

我已经学会了,因为我试图弄明白,PHP不允许数组有一个重复的键,但我想知道是否有某种类型的工作或黑客可以帮助任何想法将是非常感谢

enter code here
  <?php 
  // Finds the lowest duty rate
    $lowest = 0; 
        $row_combine = array_combine($rate_row, $totalrowcost); 
          foreach($row_combine as $a => $combine_row):
             if ($a = 0){
                $lowest = $a;
             echo ""; 
        }
            endforeach; 


   // Finds the row value associated with the lowest duty rate          
          foreach($row_combine as $a => $combine_row):
               if ($a === $lowest){
            $lowest1 =  $combine_row; 
        }
            endforeach;     

         for($i = 0; $i < count($totalrowcost); $i++) {

enter code here

    if ($totalrowcost[$i] == $lowest1){
               $totalrowcost[$i] = 0;
        }
            }?>

enter code here
 $rate_row
 Array 
  ( 
    [0] => 35 
    [1] => 0 
    [2] => 40 
    [3] => 0 
    [4] => 45 
  )


$totalrowcost
Array ( 
    [0] => 100 
    [1] => 49.99 
    [2] => 102 
    [3] => 98 
    [4] => 125 
  )

$row_combine Output
Array ( 
    [35] => 100 
    [0] => 98 
    [40] => 102 
    [45] => 125 
  )


Desired $row_combine Output
Array ( 
   [35] => 100 
   [0] =>  49.99 
   [40] => 102 
   [0] => 98
   [45] => 125 
  )


Actual Output updated 
$totalrowcost
Array ( 
  [0] => 100 
  [1] => 49.99 
  [2] => 102 
  [3] => 0 
  [4] => 125 
  ) 

Desired Output updated 
$totalrowcost
Array ( 
  [0] => 100 
  [1] => 0 
  [2] => 102 
  [3] => 0 
  [4] => 125 
 )

2 个答案:

答案 0 :(得分:0)

如果键没有重要值(即你只想稍后遍历最后一个数组),你可以通过在它们的末尾附加一个随机名称来确保键合并之前是唯一的。

这是一个在保留重复键的同时合并两个数组的函数。

function array_uniqify_keys($array,$append='',$glue='--'){
    if($append === ''){
        $append = uniqid() . mt_rand(1000,9999);
    }
    foreach($array as $key => $value){
        unset($array[$key]);
        $array[$key.$glue.$append] = $value;
    }
    return $array;
}
function array_merge_duplicate_safe($array1,$array2,$a1_name='',$a2_name=''){
    $array1 = array_uniqify_keys($array1,$a1_name);
    $array2 = array_uniqify_keys($array2,$a2_name);
    return array_merge($array1,$array2);
}
$array1 = [
    1 => 'foo',
    2 => 'boo'
];
$array2 = [
    1 => 'good',
    2 => 'bad',
    3 => 'ugly'
];
print_r(array_merge_duplicate_safe($array1,$array2));

上述代码的结果是:

Array
(
    [1--59d393950489c8380] => foo
    [2--59d393950489c8380] => boo
    [1--59d39395049197021] => good
    [2--59d39395049197021] => bad
    [3--59d39395049197021] => ugly
)

您还可以通过explode('--',$key)[0]获取旧密钥。

答案 1 :(得分:0)

documentation让课程阅读语法和有关它们的更多信息。

我对你要解决的问题有点不清楚,也许对用例的编辑可能有助于提出其他选择。

修改

  <?php
// you can use whatever input $rate_row is from user input or such along with the $totalrowcost
$rate_row = [35, 0, 40, 0, 45];
$totalrowcost = [100, 49.99, 102, 98, 125];

class Table {
  public $row_items;
  public $row_rates;
  public $row_rate_applied;    

  private $lowestRate;

  // used to init class values
  function __construct($items, $rates) {
    // set variables to class
    // might want to run validation here to make sure the count is = for $items and $rates
    $this->row_items = $items;
    $this->row_rates = $rates;
    $this->lowestRate = min($rates); // add validation to make sure rates is array http://php.net/manual/en/function.min.php

    // main guts that are used to solve your problem is inside this loop
    for($i = 0; $i < count($items); $i++) {
        // here we test if rates index == lowest. if it is set the item value to lowest
        if ($rates[$i] == $this->lowestRate) { 
            $this->row_rate_applied[$i] = $this->lowestRate;  
        } else {
            $this->row_rate_applied[$i] = $items[$i];
        }
    }
  }
 }

$table = new Table($totalrowcost, $rate_row);
echo var_dump($table->row_rate_applied);

以上代码构建并应用针对项目的费率。我并不是100%预期的结果实际应该是什么,因为最初发布的代码对预期输出

没有多大意义

基本上我们的想法是建立你的行