I came with an idea of solving it, if you don't have such a function like array_column (i.e. PHP 5.4.10)
<?php
// we assume, the data is organised in such a way: first buyer/seller/whatever then fees1/fees2/etc, item can be at the end or beginning
$a1 = [
[ 'buyer'=>"ACME", 'fees1'=>"100", 'seller'=>"XYZ plc", 'fees2'=>"200", 'item'=>"Bricks" ],
[ 'buyer'=>"ACME", 'fees1'=>"110", 'seller'=>"XYZ plc", 'fees2'=>"220", 'item'=>"Bricks" ],
[ 'buyer'=>"XYZ Plc", 'fees1'=>"200", 'seller'=>"ACME", 'fees2'=>"300", 'item'=>"Cement" ],
/* and so on and on */
];
function process($obj, $arr){ // we process a single row of $a1
$data = array(); // first we create an indexed array for storing a single key and its value
foreach($obj as $key=>$val){
$key = strtolower($key);
$val = strtolower($val);
$data[] = array($key=>$val);
}
for($i=0;$i<sizeof($data);$i++){ // we iterate over this array
$key = array_pop(array_keys($data[$i])); // we've got current key
$val = $data[$i][$key]; // we've got current value, company
if(preg_match("/buyer|seller/", $key)){ // if it's seller or buyer or whatever we want just edit
$next_key = array_pop(array_keys($data[$i+1])); // we take the next element of $data which is $key=>$value, next key i.e. fees1
$next_val = $data[$i+1][$next_key]; // we take its value e.g. 100
$arr[$val]+=$next_val; // we put the money for company, and adding them up
}
}
return $arr; // the end of processing a single row of $a1
}
$data = array(); // first we create the data split into items buckets
foreach($a1 as $obj){
foreach($obj as $key=>$val){
$key = strtolower($key); // ACME or AcMe, no difference
$val = strtolower($val);
if(preg_match("/item/", $key)){
$data[$val][] = $obj;
}
}
}
print_r($data);
$final = array(); // we store the result for items
foreach($data as $item=>$obj){ // we iterate over items
$arr = array(); // variable for one item, for next we'll start from empty array
foreach($obj as $val){ // we process each row for item
$arr = process($val, $arr);
}
$final[$item] = $arr; // we put processed data to key which is item
}
$total = array(); // we store the total result
foreach($final as $val){
foreach($val as $key=>$prize){
$total[$key]+=$prize; // we add up all money for a single company
}
}
print_r($final);
print_r($total);
?>
fees1andfees2. Would appreciate if you can include that in the question as well.