Php 将值从2个数组中除掉

Php 将值从2个数组中除掉,php,arrays,php-5.3,php-5.6,Php,Arrays,Php 5.3,Php 5.6,我有这个数组 $a = [ 0 => [ 'period' => '2017/2018', 'product' => 'AM', 'quantity_1' => 20, 'quantity' => 25, ], 1 => [ 'period' => '2018/2019', 'product'

我有这个数组

$a = [
     0 => [
         'period'     => '2017/2018',
         'product'    => 'AM',
         'quantity_1' => 20,
         'quantity'   => 25,
     ],
     1 => [
         'period'     => '2018/2019',
         'product'    => 'AM',
         'quantity_1' => 12,
         'quantity'   => 19,
     ],
     2 => [
         'period'     => '2017/2018',
         'product'    => 'DC',
         'quantity_1' => 20,
         'quantity'   => 25,
     ], 
     3 => [
         'period'     => '2018/2019',
         'product'    => 'DC',
         'quantity_1' => 8,
         'quantity'   => 10,
     ]
]
其思想是将值除以周期和乘积,在这种情况下,我们有两个产品“AM”和“DC”。因此,将2018/2019年至2017/2018年期间的值除以产品AM和产品DC。我需要得到这样的结果:

$result = [
      0 => [
            'product'     => 'AM'
            'quantity_1'  => 12/20 = 0.6
            'quantity_2'  => 19/25 = 0.76
      ],
      1 => [
            'product'     => 'DC'
            'quantity_1'  => 8/20 = 0.4
            'quantity_2'  => 10/25 = 0.4
      ]
]
$i = 0;
    foreach ($results as $result){
        $result[] = [
            'product'       => $result['product'],
            'tonnes_prod'   => $results[$i+1]['quantity_1'] / $results[$i]['quantity_1']
        ];
        $i++;
    }
我尝试了foreach,但我认为还有其他一些简单的方法可以做到这一点。如果你有什么想法,我会很感激的。谢谢你抽出时间

我试着这样做:

$result = [
      0 => [
            'product'     => 'AM'
            'quantity_1'  => 12/20 = 0.6
            'quantity_2'  => 19/25 = 0.76
      ],
      1 => [
            'product'     => 'DC'
            'quantity_1'  => 8/20 = 0.4
            'quantity_2'  => 10/25 = 0.4
      ]
]
$i = 0;
    foreach ($results as $result){
        $result[] = [
            'product'       => $result['product'],
            'tonnes_prod'   => $results[$i+1]['quantity_1'] / $results[$i]['quantity_1']
        ];
        $i++;
    }
但是我得到了一个错误:message:Notice:Undefined offset:28
首先重新制作数组,然后计算结果

$temp = [];
foreach ($a as $x){
    $temp[$x['product']][$x['period']] = $x;
}

$result = [];
foreach ($temp as $key => $res){
    $result[] = [
        'product'       => $key,
        'quantity_1'   => $res['2018/2019']['quantity_1'] / $res['2017/2018']['quantity_1'],
        'quantity_2'   => $res['2018/2019']['quantity'] / $res['2017/2018']['quantity'],
    ];
}

foreach很好。@u\u mulder我用我的foreach编辑了这个问题这部分是字符串吗?19/25=0.76看起来很难用不,结果应该是:0.76-你想这样吗?