php 2d arrays are grouped by key names and add instance functions

  • 2020-10-31 21:41:23
  • OfStack

This article introduces an example program of php 2-dimensional array grouping and adding with a certain 1 key name. If the data is taken from the database, it can be SELECT SUM(t_value), t_FROM ES8en_GROUP BY t_id. But if it is in the php program, it is a little bit more troublesome to handle similar problems

<?php 
/*  Function function: Yes 2 An array of dimensions takes some 1 Add the group names and return the new 2 Dimensional array  
 *  Parameter description: $arr- The source array; $new_arr- That's the new array; $target_key- The name of the key to group  
 */
function add_array($arr, &$new_arr, $target_key) { 
    $num = count($new_arr); // Calculate the size of the new array, and so does the new array 2 Dimension, this is the number one 1 d  
    for ($i = 0; $i < $num; $i++) { 
        // Loop through the new array  
        //if The block mainly determines whether the key name of the current group already exists in the new array to avoid duplication  
        // Since the function is called in a loop, the new array may have more than one 1 Element of, so you have to count each of the new arrays 1 All the elements are compared,  
        // The elements of the new array are 1 a 1 Dimensional array, $i The dynamic is relatively new 2 Grouping key names in dimensional arrays  
        if ($arr[$target_key] != $new_arr[$i][$target_key]) {// Determines whether the group key name in the new array is the same as the group key name in the current source array  
            $cmp_num++; // If not, the number of comparisons increases 1 
        } else {// If equal, the current grouping key name already exists  
            $tar_exist = true; // Sets the existence id to true 
            $tar_key = $i; // Returns the numeric index of the current grouping key name in the new array  
            break; // Jump out of the loop  
        } 
    } 
    // If you compare the number of times with the new array size 1 Indicates that the current grouping key name is not in the new array false 
    if ($cmp_num == $num) 
        $tar_exist = false; 
    if ($tar_exist) {// If the grouping key name already exists, add the array elements for that grouping  
        foreach ($arr as $key => $value) { 
            if ($key != $target_key) {// Element values corresponding to grouping key names do not add up  
                $new_arr[$tar_key][$key]+=$value; // The remaining element values are added  
            } 
        } 
    } else { 
        // If the grouping key name does not exist  
        // Sets the new grouping key name and adds the array elements for that grouping  
        // The first of the new array 1 D use $num Parameter to distinguish the order of the current group  
        // Due to the $num It's actually the number of keynames in the new array that are grouped from 0 Start, so the new grouping index in the new array is used directly $num Can,  
        // And don't need to $num+1 
        $new_arr[$num][$target_key] = $arr[$target_key]; 
        foreach ($arr as $key => $value) { 
            if ($key != $target_key) {// Element values corresponding to grouping key names do not add up  
                $new_arr[$num][$key]+=$value; // The remaining element values are added  
            } 
        } 
    } 
} 
$arr = array( 
    array('group_id' => 13, 'team_price' => 88.00, 'satopay_price' => 85.00, 'team_id' => 348, 'origin' => 440, 'gain' => 14.45, 'quantity' => 5), 
    array('group_id' => 13, 'team_price' => 12.00, 'satopay_price' => 11.00, 'team_id' => 344, 'origin' => 36, 'gain' => 2.76, 'quantity' => 3), 
    array('group_id' => 14, 'team_price' => 4.99, 'satopay_price' => 4.60, 'team_id' => 335, 'origin' => 4.99, 'gain' => 0.31915, 'quantity' => 1), 
    array('group_id' => 14, 'team_price' => 12.00, 'satopay_price' => 11.00, 'team_id' => 344, 'origin' => 24, 'gain' => 1.84, 'quantity' => 2), 
    array('group_id' => 15, 'team_price' => 13.00, 'satopay_price' => 11.00, 'team_id' => 344, 'origin' => 24, 'gain' => 1.84, 'quantity' => 2), 
); 
$new_arr = array(); 
foreach ($arr as $key => $value) { 
    add_array($value, &$new_arr, 'group_id'); // Here we press group_id Add them in groups  
} 
var_dump($new_arr);

Related articles: