简体   繁体   English

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

[英]How to merge 2 objects in array in php

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

both have same content so how to merge this both so that i wil get data like this:两者都有相同的内容,所以如何合并这两者,以便我得到这样的数据:

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

Code:-代码:-

$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);
}

with above code i am getting this json but i want to change its format as i said earlier in question:使用上面的代码,我得到了这个 json,但我想改变它的格式,正如我之前所说的:

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

I want to change about output with this actually:我想用这个实际上改变输出:

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

Change your code like below:-更改您的代码,如下所示:-

1.Only single array required 1.只需要一个数组

2.Use monthss_year as key (so that common values can merge easily) 2.使用monthss_year作为key(这样常见的值可以很容易地合并)

3.Add common monthss_year org as comma seperated value 3.添加常用的monthss_year org作为逗号分隔值

4.Add common monthss_year action as comma seperated value monthss_year常用的monthss_year action作为逗号分隔值

5.From final array remove monthss_year keys and re-index it as numeric array (like 0,1,2,...) monthss_year从最终数组中删除monthss_year键并将其重新索引为数字数组(如0,1,2,...)

6.Encode the array and print to see final result 6.对数组进行编码并打印以查看最终结果

Code need to be like below:-代码需要如下所示:-

$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);

Note:- What data you shown for print_r($query1->result()) , for that my edited code is working perfectly fine:-注意:- 您为print_r($query1->result()) ,因为我编辑的代码运行良好:-

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

Try using尝试使用

$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);

You can try this, i think it will work.你可以试试这个,我认为它会起作用。

$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);

output:输出:

Array ( [month] => FEBRUARY [year] => 2018 [org] => 40,41 [action] => 4,5 )数组 ( [月] => 二月 [年] => 2018 [组织] => 40,41 [动作] => 4,5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM