Skip to content
Advertisement

How to Combine 2 Array with same key

I have 2 array like this …

$a =  [
  ["Name" => "DH", "TotalSel" => 11700.0],
  ["Name" => "PD", "TotalSel" => 7000.0],
  ["Name" => "AT", "TotalSel" => 3000.0],
  ["Name" => "CC", "TotalSel" => 2400.0]
]

$b = [
  ["Name" => "PD", "TotalBuy" => 7800.0],
  ["Name" => "EP", "TotalBuy" => 7000.0],
  ["Name" => "CC", "TotalBuy" => 6900.0],
  ["Name" => "AT", "TotalBuy" => 2400.0]
]

Then I want to combine and group all the same key (“Name”), so that the end result looks like this…

$result =  [
  ["Name" => "DH", "TotalSel" => 11700.0, "TotalBuy" => 0.0],
  ["Name" => "PD", "TotalSel" => 7000.0, "TotalBuy" => 7800.0],
  ["Name" => "AT", "TotalSel" => 3000.0, "TotalBuy" => 2400.0],
  ["Name" => "CC", "TotalSel" => 2400.0, "TotalBuy" => 6900.0],
  ["Name" => "EP", "TotalSel" => 0.0, "TotalBuy" => 7000.0]
]

How can I achieve this?

Advertisement

Answer

I created distinct names array. And then in a for loop for each of the names, searched the index for that name in both array $a and $b. That gave the values for total sell and total buy for each names. And created array $c with relevant information.

$a =  [
  ["Name" => "DH", "TotalSel" => 11700.0],
  ["Name" => "PD", "TotalSel" => 7000.0],
  ["Name" => "AT", "TotalSel" => 3000.0],
  ["Name" => "CC", "TotalSel" => 2400.0]
];

$b = [
  ["Name" => "PD", "TotalBuy" => 7800.0],
  ["Name" => "EP", "TotalBuy" => 7000.0],
  ["Name" => "CC", "TotalBuy" => 6900.0],
  ["Name" => "AT", "TotalBuy" => 2400.0]
];

$c = [];


// make all names array
$names = [];
for($i=0; $i<count($a); $i++) {
    //echo $a[$i]['Name'] .", ". $a[$i]['TotalSel']."<br>";
    $names[] = $a[$i]['Name'];
}
for($i=0; $i<count($b); $i++) {
    //echo $b[$i]['Name'] .", ". $b[$i]['TotalBuy']."<br>";
    if(!in_array($b[$i]['Name'], $names)) {
        $names[] = $b[$i]['Name'];
    }
}

$finalIndex=0;
foreach($names as $key=>$val) {
    //echo $val ."<br>";
    
    $index = searchArray($a, "Name", $val);
    if($index!=-1) {
        $sel = $a[$index]['TotalSel'];
    }
    else {
        $sel = 0;
    }  
    $index = searchArray($b, "Name", $val);
    if($index!=-1) {
        $buy = $b[$index]['TotalBuy'];
    }
    else {
        $buy = 0;
    } 
    
    $c[$finalIndex]['Name']=$val;
    $c[$finalIndex]['TotalSel'] = $sel;
    $c[$finalIndex]['TotalBuy'] = $buy; 
    
    $finalIndex++;            
}


for($i=0; $i<count($c); $i++) {
    echo $c[$i]['Name'] .", ".$c[$i]['TotalSel'].",". $c[$i]['TotalBuy']."<br>";
}

function searchArray($arr,$field,$value) {
    //echo "<br>-----------------------field = $field, value = $value <br>";
    $j=0;
    foreach($arr as $key ) {
        foreach ($key as $key1 => $val1) {
            //echo "$key1, $val1 , $j<br>";
            if($key1 == $field && $val1 == $value ) {                        
                return $j; // array index
            }
            
        }
        $j++;
    }
    
    return -1;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement