函数php来组合数组

时间:2016-11-24 00:58:48

标签: php arrays

我应该使用什么功能将array 0与基于array 1的{​​{1}}结合起来。如果值extrefextrefarray 0相同,则数组的组合为真。

array 1

结果应如下所示: -

Array
([Transaction] => Array
        ([0] => Array //firts index array
                ([extref] => WVB4108-10002761
                    [Denominations] => Array
                        ([Denomination] => Array
                                ([0] => Array
                                        (
                                            [denomvalue] => 0.10
                                            [denomqty] => 5
                                            [denomcount] => 0.50
                                        )

                                    [1] => Array
                                        (
                                            [denomvalue] => 0.50
                                            [denomqty] => 2
                                            [denomcount] => 1.00
                                        )

                                ))))

            [1] => Array //second index array
                ([extref] => WVB4108-10002761
                    [Denominations] => Array
                        ([Denomination] => Array
                                (
                                    [denomvalue] => 1.00
                                    [denomqty] => 13
                                    [denomcount] => 13.00
                                )

                        ))

3 个答案:

答案 0 :(得分:0)

比较两个extref,如果它们相同则使用array_merge

if ($array['Transaction'][0]['extref'] == $array['Transaction'][1]['extref']) {
    $array['Transaction'][0]['Denominations']['Denomination'] = 
        array_merge($array['Transaction'][0]['Denominations']['Denomination'], $array['Transaction'][1]['Denominations']['Denomination']);
        unset($array['Transaction'][1]);
}

答案 1 :(得分:0)

RegistrationPlate

答案 2 :(得分:0)

Here是通过任意交易实现此目标的一种方法:

function cmp($a, $b) {
    if ($a['extref'] == $b['extref'])
        return 0;
    return ($a['extref'] < $b['extref']) ? -1 : 1;
}

uasort($arr['Transaction'], 'cmp');

for($i = 0; $i < count($arr['Transaction'])-1 ; $i++){
    if ($arr['Transaction'][$i]['extref'] == $arr['Transaction'][$i+1]['extref']){
        $arr['Transaction'][$i] = array_merge_recursive($arr['Transaction'][$i],array_splice($arr['Transaction'],$i+1,1)[0]);
        $arr['Transaction'][$i]['extref'] = reset($arr['Transaction'][$i]['extref']);
    }
}