2
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:

{"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"}
3
  • Have you tried anything that failed? Commented Feb 21, 2018 at 10:13
  • I have updated my answer, please try Commented Feb 21, 2018 at 10:42
  • If you use mysql, do that by group_concat in query Commented Feb 21, 2018 at 10:42

3 Answers 3

1

Change your code like below:-

1.Only single array required

2.Use monthss_year as key (so that common values can merge easily)

3.Add common monthss_year org as comma seperated value

4.Add common monthss_year action as comma seperated value

5.From final array remove monthss_year keys and re-index it as numeric array (like 0,1,2,...)

6.Encode the array and print to see final result

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:-

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

Sign up to request clarification or add additional context in comments.

1 Comment

@amitsutar i have changed my code a bit please check once and try that-one . As it's working for me for your shown data:-eval.in/960167
0

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

2 Comments

@amitsutar I have updated my answer, please try again
@amitsutar ok I think this should work. I have updated my answer again
0

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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.