dongpalou5352
dongpalou5352
2012-06-11 18:58

在PHP中组合子数组

已采纳

I am trying to combine array(s) by key. So all sub arrays that start with the same sub-key will be combined into one sub array. Also, I want to keep the smallest value of the matched values.

Currently this is what I have:

$statement = Array
(
    [662_0] => Array
        (
            [0] => 06-01-2012
            [1] => 436
            [2] => MEDIA
            [3] => 2006
            [4] => Template Testing
            [5] => KS
            [6] => 662
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 1290
            [13] => 1290.00
            [14] => 0.00
            [15] => 0
            [16] => 1290.00
        )

    [662_1] => Array
        (
            [0] => 06-01-2012
            [1] => 436
            [2] => MEDIA
            [3] => 2006
            [4] => Template Testing
            [5] => KS
            [6] => 662
            [7] => 295.00
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 995
            [13] => 1290.00
            [14] => 0.00
            [15] => 0
            [16] => 1290.00
        )

    [662_2] => Array
        (
            [0] => 06-01-2012
            [1] => 436
            [2] => MEDIA
            [3] => 2006
            [4] => Template Testing
            [5] => KS
            [6] => 662
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 1290
            [13] => 1290.00
            [14] => 0.00
            [15] => 0
            [16] => 1290.00
        )

    [663_0] => Array
        (
            blah blah blah...
        )
);

This is what I would like to have:

$statement = Array
(
    [662] => Array
        (
            [0] => 06-01-2012
            [1] => 436
            [2] => MEDIA
            [3] => 2006
            [4] => Template Testing
            [5] => KS
            [6] => 662
            [7] => 295.00
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 995
            [13] => 1290.00
            [14] => 0.00
            [15] => 0
            [16] => 1290.00
        )

    [663] => Array
        (
            blah blah blah...
        )
);

Happy to provide more info if needed! Thanks!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongshao6591 dongshao6591 9年前

    This code iterates over your original array, cuts by _ to get the combined identifier for the items and merges them together, and replaces the value in the resulting array if the new value is smaller than the one that is already set.

    <?php
    $original = array(
            '122_0' => array('k1'=>4, 'k2'=>2),
            '122_1' => array('k2'=>3, 'k3'=>3),
            '122_2' => array('k3'=>2, 'k4'=>4),
            '111_0' => array('k1'=>3, 'k2'=>3),
            '111_1' => array('k2'=>2, 'k3'=>2),
            '111_2' => array('k3'=>1, 'k4'=>1)
    );
    
    foreach ($original as $key=>$sub) {
        list($realkey, $num) = explode('_', $key);
        foreach ($sub as $subkey => $value) {
            if ( isset($result[$realkey][$subkey]) 
                 && $value < $result[$realkey][$subkey] ) {
                $result[$realkey][$subkey] = $value;
            }
        }
    }
    print_r($result);
    
    点赞 评论 复制链接分享