简体   繁体   中英

Dynamic associative Array - list, count, sum, min, max

I've got an array with about 40 keys. I'd like to have a small function that returns a summary array.

Right now I've got the following that works:

foreach ($all_data as $value){
    $new_array[ $value['location'] ][ $value['manufacturer'] ][ $value['model'] ] += 1;
}

This returns an array with everything I need. However, the location, manufacturer and model could be changed up for a bunch of other values.

what I am trying to do is have something simple as:

$new_array = summarize($all_data,array('location','manufacturer','model','count'),array('list','list','list','count') );}

where this summarize function would build the call. I think I just need a bit of help on how to get it to run the string as code for this array. Otherwise I get

$current_selection = "[ $row_item['location'] ][ $row_item['manufacturer'] ][ $row_item['model'] ]"
$return_array{$current_selection} += 1;

Where the end goal is to have a function like:

function summarize($data_array, $fields_array, $process_array){
    //data_array    = associative multi-dimensional data array
    //fields    = values to pull from the data_array
    //process   = array specifying whether to list, sum, count, average, max, min

$return_array = array();
$current_selection = "";
foreach($fields_array as $field){
    $current_selection .= '[ $row_item[\'' . $field . '\'] ]';
}

    foreach ($data_array as $row_item){

//dynamic = DOES NOT WORK
        $return_array[$current_selection] += 1;//eval? create function? abstract?
        //another attempt
${'return_array' . $current_selection} += 1;
//Manual = Does work
        //$return_array[    $row_item['location']  ][   $row_item['manufacturer']  ][   $row_item['model']  ] += 1;
    }
}

Thanks for any help on how to do an indirect reference.

JC

RESOLUTION The final version that managed to resolve this looks like the following, thanks to user: check, for getting me on the correct path.

function summarize($data_array, $fields_array, $process_array){
    $return_array = array();
    $i = 0;
    foreach ($data_array as $row){
    $ii = 0;
        $temp = array();
        $temp2 = array();
        foreach($fields_array as $key=>$field){
            if($process_array[$ii] == 'list')   $temp[$ii] = $row[$field];
        if($process_array[$ii] == 'count')  $temp2[$ii] = 1;
        if($process_array[$ii] == 'sum')    $temp2[$ii] = $row[$field];
        $ii++;
        }

        $unique = true;
        $ii = 0;
        foreach($return_array as $row2){
            if(array_intersect_key($row2,$temp) == $temp){//$row2 == $temp){
                $unique = false;
                break;
            }
            $ii++;
        }

        if($unique){
            $return_array[$i] = $temp;
            if(!empty($temp2)) $return_array[$i] = array_merge($temp,$temp2);
            $i++;
    }else{
        if(!empty($temp2)){
            foreach($temp2 as $key => $value){
                if($process_array[$key] == 'sum')   $temp2[$key] = $return_array[$ii][$key] + $value;
                if($process_array[$key] == 'count') $temp2[$key] = $return_array[$ii][$key] + 1;
                if($process_array[$key] == 'max')   $temp2[$key] = ($return_array[$ii][$key] < $value) ? $value : $return_array[$ii][$key];
                if($process_array[$key] == 'min')   $temp2[$key] = ($return_array[$ii][$key] > $value) ? $value : $return_array[$ii][$key];
                //TODO:(JC) 'average' - need to create a count field if not present (or always despite and assume overhead of extra computations).
                //            - then just calculate the 'sum' and divide by the counter as a last step before returning the array.
            }
            $return_array[$ii] = array_merge($temp,$temp2);
        }
    }
    }
        print_r($return_array);
    return $return_array;
}

Which gives the following result:

/*
CALL: summarize($data,array('location','manufacturer','model','model','volume','colourvolume'),array('list','list','list','count','sum','sum') );
    [0] = location
    [1] = manufacturer
    [2] = model
    [3] = model count
    [4] = mono volume sum
    [5] = colour volume sum
 */
Array
(
    [0] => Array
        (
            [0] => 
            [1] => HP
            [2] => LaserJet 4000
            [3] => 3
            [4] => 3000
            [5] => 0
        )
    ...

    [17] => Array
        (
            [0] => Room 114
            [1] => CANON
            [2] => iR3235
            [3] => 1
            [4] => 4012
            [5] => 0
        )

    [18] => Array
        (
            [0] => Room 115
            [1] => LEXMARK
            [2] => T652
            [3] => 1
            [4] => 20
            [5] => 0
        )

)

alternatively, if I assume that's $field_array contains sequentially key fields from root to sub key, you can loop your $field_array within $data_array loop

function summarize($data_array, $fields_array, $process_array){
    $return_array = array();
    foreach ($data_array as $row){
        $temp = array();
        foreach($fields_array as $key=>$field){
            $temp = $key==0?$row[$field]:$temp[$field];
        }
        if(!empty($temp)) $return_array[] = $temp;
    }
    return $return_array;
}

and this is my array, will summarize with these function

$array = array(
    array("multi"=>array("dimensional"=>array("array"=>"foo1"))),
    array("multi"=>array("dimensional"=>array("array"=>"foo2"))),
    array("multi"=>array("dimensional"=>array("array"=>"foo3"))),
    array("multi"=>array("dimensional"=>array("array"=>"foo4"))),
    array("multi"=>array("dimensional"=>array("array"=>"foo5"))),
    array("multi"=>array("dimensional"=>array("array"=>"foo6"))),
    array("multi"=>array("dimensional"=>array("array"=>"foo7"))),
    array("multi"=>array("dimensional"=>array("array"=>"foo8"))),
    array("multi"=>array("dimensional"=>array("array"=>"foo9")))
);
print_r(summarize($array,array("multi","dimensional","array"),NULL));

Ouput

Array ( [0] => foo1 [1] => foo2 [2] => foo3 [3] => foo4 [4] => foo5 [5] => foo6 [6] => foo7 [7] => foo8 [8] => foo9 ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM