Puvi
Puvi

Reputation: 41

how to combine 2 array values into single one by common value

1.fetching the values from a same table with different date

 <?php 
    $date= date("Y-m-d");;
    $prev_date = date('Y-m-d', strtotime($date .' -1 day'));
    $result_yest=mysql_query("SELECT isp,sum(sent) AS `sent_yest` FROM red_global WHERE status_date='$prev_date' and sent > 0 group by 1",$link1);
    $data_yest = array(); // create a variable to hold the information
    while (($row_yest = mysql_fetch_array($result_yest, MYSQL_ASSOC)) !== false){
      $data_yest[] = $row_yest; // add the row in to the results (data) array
    }
    print_r($data_yest); 
    $result_today=mysql_query("SELECT isp,sum(sent) AS `sent_today` FROM red_global WHERE status_date='$date' and sent > 0 group by 1",$link1);
    $data_today = array(); // create a variable to hold the information
    while (($row_today = mysql_fetch_array($result_today, MYSQL_ASSOC)) !== false){
      $data_today[] = $row_today; // add the row in to the results (data) array
    }
    print_r($data_today); 
    exit;
?>

2.Array results in which one array contains yesterday's sent count and another one aray contains today's sent count both array having the common isp

Array ( 
  [0] => Array ( [isp] => aol [sent_yest] => 46838 ) 
  [1] => Array ( [isp] => gmail [sent_yest] => 33015 ) 
  [2] => Array ( [isp] => juno [sent_yest] => 93544 ) 
  [3] => Array ( [isp] => roadrunner [sent_yest] => 6181 ) 
  [4] => Array ( [isp] => yahoo [sent_yest] => 71444 ) 
)

Array ( 
  [0] => Array ( [isp] => aol [sent_today] => 14135 ) 
  [1] => Array ( [isp] => att [sent_today] => 624 ) 
  [2] => Array ( [isp] => gmail [sent_today] => 21263 ) 
  [3] => Array ( [isp] => juno [sent_today] => 74934 ) 
  [4] => Array ( [isp] => roadrunner [sent_today] => 939 ) 
  [5] => Array ( [isp] => yahoo [sent_today] => 22059 ) 
) 

Now i need a result like this isp name, yesterday's sent count, today's sent count

[isp, sent_yest, sent_today],
[aol, 46838,  14135],
[att, 0,  624],
[gmail, 33015,  21263],

Anyone help me to solve this.. Thanks in advance

Upvotes: 1

Views: 75

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Do it like below:-

$final_array = array();

if(count($arr1)>=count($arr2)){
  foreach ($arr1 as $arr){
      $key='';
      foreach ($arr2 as $ar2){
         if($arr['isp'] == $ar2['isp']){
            $final_array[$arr['isp']] = array($arr['isp'],$arr['sent_yest'],$ar2['sent_today']);
            $key ='';break;
         }else{
             $key = $arr['isp'];
         }
      }
      if($key!==''){
            $final_array[$key] = array($arr['isp'],$arr['sent_yest'],0);
            $key ='';
      }
  }
}

if(count($arr2)>count($arr1)){
   foreach ($arr2 as $ar2){
       $key='';
      foreach ($arr1 as $arr){
         if($ar2['isp'] == $arr['isp']){
            $final_array[$ar2['isp']] = array($ar2['isp'],$arr['sent_yest'],$ar2['sent_today']);
             $key ='';break;
         }else{
             $key = $ar2['isp'];
         }
      }
      if($key!==''){
            $final_array[$key] = array($ar2['isp'],0,$ar2['sent_today']);
            $key ='';
      }
   }
}

echo "<pre/>";print_r($final_array);

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

Upvotes: 2

JuhG
JuhG

Reputation: 138

I would avoid foreach if it's possible, since it leads to code that's much harder to understand. With array operation it can look like this:

// First we merge the two arrays
$merged = array_merge($data_yest, $data_today);

// Then format it
$final = array_reduce($merged, function($final, $item) {

    // Initialize for each isp if it's not present
    // This way we avoid overwriting previous data and have a default value 0
    if (! isset($final[ $item['isp'] ])) {
        $final[ $item['isp'] ] = [
            'sent_yest' => 0,
            'sent_today' => 0,
        ];
    }

    // And if one of the days is present, we add it
    if (isset($item['sent_yest'])) {
        $final[ $item['isp'] ][ 'sent_yest' ] = $item['sent_yest'];
    }
    if (isset($item['sent_today'])) {
        $final[ $item['isp'] ][ 'sent_today' ] = $item['sent_today'];
    }

    // Then return the modified array
    return $final;
}, []);

print_r($final);
exit;

Result looks like this:

Array
(
    [aol] => Array
        (
            [sent_yest] => 46838
            [sent_today] => 14135
        )

    [gmail] => Array
        (
            [sent_yest] => 33015
            [sent_today] => 21263
        )

    [juno] => Array
        (
            [sent_yest] => 93544
            [sent_today] => 74934
        )

    [roadrunner] => Array
        (
            [sent_yest] => 6181
            [sent_today] => 939
        )

    [yahoo] => Array
        (
            [sent_yest] => 71444
            [sent_today] => 22059
        )

    [att] => Array
        (
            [sent_yest] => 0
            [sent_today] => 624
        )

)

Upvotes: 2

Related Questions