【问题标题】:merge array on basis on spefic key PHP基于特定键 PHP 合并数组
【发布时间】:2015-06-30 04:53:20
【问题描述】:
Array
(
    [0] => Array
        (
            [employee] => 15
            [first_name] => Person1
            [surname] => Person1
            [totaltime] => 183.75
            [FirstValue] => 1
        )

[1] => Array
    (
        [employee] => 15
        [first_name] => Person1
        [surname] => Person1
        [totaltime] => 183.75
        [SecondValue] => 2            
    )
)

我想要这样的输出

Array
(
    [0] => Array
        (
            [employee] => 15
            [first_name] => Person1
            [surname] => Person1
            [totaltime] => 183.75
            [FirstValue] => 1
            [SecondValue] => 2
        )
)

我有一个多维数组。每个元素都有一个员工作为键,键可以相同也可以不同。我想用相同的EmployeeID 合并数组。另外 2 名相同的员工可能有不同的元素,如 FirstValueSecondValue 等。我想合并上面输出中显示的唯一元素。

【问题讨论】:

  • 这是什么语言?数组无处不在... PHP 标签?
  • @bonCodigo ofcoz PHP
  • 我的问题是针对 OP!或者其他人已经考虑编辑了大约 8 分钟......但没有 PHP 标记。
  • 您是从数据库中提取这些数据,而这实际上是您的 SQL 查询的结果吗?如果是这样,取决于您使用的是哪种驱动程序,请查看 FETCH ASSOCIATIVE 数组,这可以解决您的问题。另一件事是,由于键值是重复的,并且这是多维数组,您可以删除不奇数的第一维键。

标签: php arrays merge


【解决方案1】:
$arr = Array(
   Array
        (
            employee => 15,
            first_name => 'Person1',
           surname => 'Person1',
            totaltime => 183.75,
            FirstValue => 1
        ),
 Array
    (
        employee=> 15,
        first_name => Person1,
        surname=> Person1,
        totaltime => 183.75,
        SecondValue => 2            
    ),
       Array
        (
            employee => 16,
            first_name => 'Person1',
           surname => 'Person1',
            totaltime => 183.75,
            FirstValue => 1
        ));

$ids = array();                            // array employee => key

foreach($arr as $key => &$item)
   if(isset($ids[$item['employee']])) {    // is there such employee in ids
      $arr[$ids[$item['employee']]] =      // yes - add values from current item
          array_replace($arr[$ids[$item['employee']]], $item);  
      unset($arr[$key]);                   // and remove it from array
   }
   else $ids[$item['employee']] = $key;    // save current key

print_r($arr);

结果

Array
(
    [0] => Array
        (
            [employee] => 15
            [first_name] => Person1
            [surname] => Person1
            [totaltime] => 183.75
            [FirstValue] => 1
            [SecondValue] => 2
        )

    [2] => Array
        (
            [employee] => 16
            [first_name] => Person1
            [surname] => Person1
            [totaltime] => 183.75
            [FirstValue] => 1
        )

)

【讨论】:

    【解决方案2】:
    $output = array_merge($array1, $array2);
    

    查看array_merge().

    【讨论】:

    • 亲爱的,它是一个单一的多任务阵列。并且合并应该在公共特定密钥的基础上进行。
    【解决方案3】:

    使用这个:

    $data = array(); // your data
    
    array_merge($data[0],$data[1]); //merge it to first data
    unset($data[1]); // and unset the second data with 
    echo '<pre>';
    print_r($data);
    echo '<pre>';
    

    【讨论】:

    • 先生,这个数组是动态的而不是静态的。
    • @user375947 但格式总是有 $data[0] 和 $data[1] 或者那里可以有很多数据?
    【解决方案4】:
    $result = [];
    foreach ($array as $employee) {
        if (isset($result[$employee['employee']])) {
            $result[$employee['employee']] += $employee;
        } else {
            $result[$employee['employee']] = $employee;
        }
    }
    

    使用数组键是唯一的这一事实(在$result)来删除和合并值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 2021-07-25
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多