php合并同名值

时间:2019-03-04 11:18:51

标签: php codeigniter

我得到这个作为输出

Department  Employee Name   Employee Code   
Sales   Ganeshmurthy    264 
Sales   AZIM ahmed KHAN 2012    
Sales   DHANABAL    231 04-03-2019  
Service RAVIKIRAN   2003    04-03
Manufacturing   RAJAVARMAN  2073    04-03-2019  09:20   1h 10m
Manufacturing   MEGALA  332 04-03-2019  08:14   0h 4m
Manufacturing   SABARI  333 04-03-2019  08:13   0h 3m
Manufacturing   Sasi    1001    04-03-2019  08:16   0h 6m
Manufacturing   FRANCIES XAVIER 136 04-03-2019  08:13   0h 3m
Manufacturing   MANIKANDAN.V    151 04-03-2019  08:15   0h 5m

我得到的是td格式,我需要将重复项合并为一个吗? 我有一个包含多个部门的数组,我需要将相同的部门名称合并到一个数组中。例如,如果有department1department1department1重复n次。我需要将其合并为department1的一个数组。我该如何实现呢?我需要在表格中显示-td,这样部门就不会重复了

[0] => Array
        (
            [department_id] => 15
            [department_name] => R&D
            [name] => SUBRAMANIAN
            [code] => 101
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [1] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => RAVI
            [code] => 103
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [2] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => AMULRAJAN.A
            [code] => 104
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [3] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => KANAKARAJ.R
            [code] => 105
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

    [4] => Array
        (
            [department_id] => 3
            [department_name] => Manufacturing
            [name] => ELAYARAJA
            [code] => 106
            [clockin] => 
            [late] => 
            [date] => 1970-01-01
        )

预期结果如下

[1] => Array
    (

        [department_name] => Manufacturing

    )

[2] => Array
    (

        [department_name] => R & D

    )

这是我尝试过的:

foreach($attendance as $dr){
           $alldep = $dr->department_name;
       echo $alldep;
}

请注意,我获取的所有数据都是数组格式,而不是作为对象

4 个答案:

答案 0 :(得分:1)

您应该这样尝试

$uniqArr= array();
foreach ($attendance as $dr) {
    $uniqArr[] = $dr->department_name;;
}
$uniqueRes = array_unique($uniqArr);

答案 1 :(得分:0)

采用新的数组$departments

遍历给定数组。

department附加到$departments

使用department作为密钥。

由于PHP数组始终具有唯一键,因此您只会获得唯一的部门。

工作代码:

<?php
$arr = [
[
'department_id' => 15 ,'department_name' => 'R&D' ,'name' => 'SUBRAMANIAN' ,'code' => 101
 ,'clockin' => '' ,'late' => '' ,'date' => '1970-01-01'
],
[
 'department_id' => 3 ,'department_name' => 'Manufacturing' ,'name' => 'RAVI' ,'code' => 103
 ,'clockin' => '' ,'late' => '' ,'date' => '1970-01-01'
],
[
 'department_id' => 3 ,'department_name' => 'Manufacturing' ,'name' => 'AMULRAJAN.A' ,'code' => 104 
 ,'clockin' => '' ,'late' =>'' ,'date' => '1970-01-01'
],
[
 'department_id' => 3 ,'department_name' => 'Manufacturing' ,'name' => 'KANAKARAJ.R' ,'code' => 105
 ,'clockin' => '' ,'late' => '' ,'date' => '1970-01-01'
],
[
 'department_id' => 3 ,'department_name' => 'Manufacturing' ,'name' => 'ELAYARAJA' ,'code' => 106
 ,'clockin' => '' ,'late' => ''
]];

$departments = '';
if (! empty($arr)) {
    foreach ($arr as $elem) {
 $departments[$elem['department_name']] = $elem['department_name'];
    }
}
echo '<pre>';print_r($departments);echo '</pre>';

输出:

Array
(
    [R&D] => R&D
    [Manufacturing] => Manufacturing
)

答案 2 :(得分:0)

尝试使用此代码获取所需的输出

$arr = array_unique(array_column($attendance, 'department_name'));
foreach($arr AS $a)
{
    $newarr[]['department_name'] = $a;
}
echo "<pre>"; print_r($newarr);

答案 3 :(得分:0)

尝试一下

$department_names = array();

 foreach($attendance as $dr){
   $alldep = $dr->department_name;
   array_push($department_names , array('department_name' => $dr->department_name)); 
}

print_r($department_names$department_names);

输出

[1] => Array
(
    [department_name] => Manufacturing
)
[2] => Array
(
    [department_name] => R & D
)