如何在php中合并数组中的2个对象

时间:2018-02-21 10:09:31

标签: php arrays

Array
(
    [month] => FEBRUARY
    [year] => 2018
    [org] => 40
    [action] => 4
)
Array
(
    [month] => FEBRUARY
    [year] => 2018
    [org] => 41
    [action] => 5
)

两者都有相同的内容,所以如何合并这两者,以便我得到这样的数据:

{"month":"FEBRUARY","year":"2018","org":"40,41","action":"4,5"}

代码: -

$query1 = $this->db->query($queryString);      
$children = array();
$yearArray = array(); 
foreach ($query1->result() as $data1)
{                               

    $yearArray['month'] = $data1->months;
    $yearArray['year'] = $data1->PAY_YEAR;                       
    $yearArray['org'] = $data1->org;                            
    $yearArray['action'] = $data1->action;                                
    print_r($result);
    array_push($children, $yearArray);
}

上面的代码我得到了这个json,但我想改变它的格式,正如我之前所说的那样:

{"month":"FEBRUARY","year":"2018","org":"40","action":"4"},{"month":"MARCH","year":"2018","org":"40","action":"5"}

我想实际改变输出:

{"month":"FEBRUARY","year":"2018","org":"40,41","action":"4,5"}

3 个答案:

答案 0 :(得分:1)

更改您的代码如下: -

1.仅需要单个阵列

2.使用monthss_year作为键(以便公共值可以轻松合并)

3.以逗号分隔值

添加常用monthss_year org

4.以常用monthss_year action添加逗号分隔值

5.从最终数组中删除monthss_year个键并将其重新索引为数字数组(如0,1,2,...)

6.编码数组并打印以查看最终结果

代码必须如下所示: -

$query1 = $this->db->query($queryString);      
$children = array(); // only single array required
foreach ($query1->result() as $data1)
{                               

    $children[trim($data1->monthss).'_'.trim($data1->PAY_YEAR)]['month'] = $data1->months;
    $children[trim($data1->monthss).'_'.trim($data1->PAY_YEAR)]['year'] = $data1->PAY_YEAR; 

    $children[trim($data1->monthss).'_'.trim($data1->PAY_YEAR)]['org'] = (isset($children[trim($data1->monthss).'_'.trim($data1->PAY_YEAR)]['org'])) ? $children[trim($data1->monthss).'_'.trim($data1->PAY_YEAR)]['org'].','.$data1->org : $data1->org;

    $children[trim($data1->monthss).'_'.trim($data1->PAY_YEAR)]['action'] = (isset($children[trim($data1->monthss).'_'.trim($data1->PAY_YEAR)]['action'])) ? $children[trim($data1->monthss).'_'.trim($data1->PAY_YEAR)]['action'].','.$data1->action : $data1->action;
}

$children = array_values($children);

echo json_encode($children);

注意: - 您为print_r($query1->result())显示了哪些数据,因为我编辑的代码工作正常: -

输出: - https://eval.in/960167

答案 1 :(得分:0)

尝试使用

$query1 = $this->db->query($queryString);      
$children = array();

$temp = array();
foreach ($query1->result() as $data1)
{                          
    if(!isset($temp[$data1->PAY_YEAR.$data1->months]) {
       $temp[$data1->PAY_YEAR.$data1->months] = array();
    }  
    $yearArray = array();
    $yearArray['month'] = $data1->months;
    $yearArray['year'] = $data1->PAY_YEAR;                       
    $yearArray['org'] = $data1->org;                            
    $yearArray['action'] = $data1->action;                          

    $temp = array_merge_recursive($temp[$data1->PAY_YEAR.$data1->months], $yearArray);
}
print_r($temp);

答案 2 :(得分:0)

你可以试试这个,我认为它会起作用。

$a = array("month"=>"FEBRUARY", "year"=>2018, "org"=>40, "action"=>4);
$b = array("month"=>"FEBRUARY", "year"=>2018, "org"=>41, "action"=>5);
$k = $m = 0;
foreach($a as $key => $val) {
  if($val == $b[$key] ) {
      if($b['org'] != $a['org'] && $k == 0) {
        $a['org'] = $a['org'].",".$b['org'];
        $k++;
      }
      if($b['action'] != $a['action'] && $m == 0) {
        $a['action'] = $a['action'].",".$b['action'];
        $m++;
      }
  }
}
print_r($a);

输出:

数组([month] => FEBRUARY [year] => 2018 [org] => 40,41 [action] => 4,5)