动态关联数组 - 列表,计数,总和,最小值,最大值 [英] Dynamic associative Array - list, count, sum, min, max

查看:164
本文介绍了动态关联数组 - 列表,计数,总和,最小值,最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有40个键的数组。我想要一个返回一个汇总数组的小函数。



现在我已经得到以下工作:

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

这将返回一个我需要的一切数组。但是,位置,制造商和型号可以更改一些其他值。



我想要做的是简单的:

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

函数将构建调用。我想我只需要一些帮助,如何让它运行字符串作为这个数组的代码。否则我得到

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

最终目标是要具有如下功能:

 函数汇总($ data_array,$ fields_array,$ process_array){
// data_array =关联多维数据数组
// fields = values从data_array
// process = array指定是否列表,sum,count,average,max,min

$ return_array = array();
$ current_selection =;
foreach($ fields_array as $ field){
$ current_selection。='[$ row_item [\''。 $字段。 '\']]';
}

foreach($ data_array as $ row_item){

// dynamic = DOES NOT WORK
$ return_array [$ current_selection] + = 1 ; // EVAL?创建功能?抽象?
//另一个尝试
$ {'return_array'。 $ current_selection} + = 1;
//手动=工作
// $ return_array [$ row_item ['location']] [$ row_item ['manufacturer']] [$ row_item ['model']] + = 1;
}
}

感谢任何关于如何做间接引用的帮助。



JC



解决方案
管理的最终版本解决这个看起来像以下,感谢用户:检查,让我在正确的路径。

 函数总结($ 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 =假;
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' - 如果不存在,则需要创建一个计数字段(或者总是假设额外计算的开销)。
// - 然后只需计算'sum',并将计数器除以返回数组之前的最后一步。
}
$ return_array [$ ii] = array_merge($ temp,$ temp2);
}
}
}
print_r($ return_array);
return $ return_array;
}

其中给出以下结果:

  / * 
CALL:总结($ data,array('location','manufacturer','model','model','volume' colourvolume'),array('list','list','list','count','sum','sum'));
[0] =位置
[1] =制造商
[2] =模型
[3] =模型计数
[4] =单声道总和
[5] =颜色体积总和
* /
数组

[0] =>数组

[0] => ;
[1] => HP
[2] => LaserJet 4000
[3] => 3
[4] => 3000
[5] => 0

...

[17] =>数组

[0] =& 114
[1] => CANON
[2] => iR3235
[3] => 1
[4] => 4012
[ 5] => 0


[18] =>数组

[0] => => LEXMARK
[2] => T652
[3] => 1
[4] => 20
[5] => 0




解决方案

或者,如果我假设 $ field_array 包含从根到子键的顺序关键字段,可以循环 $ field_array $ $ data_array 循环

函数summary($ 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;
}

这是我的数组,将使用这些函数进行总结

  $ array = array(
array(multi=> array(dimensional=> array(array= "foo1)))
array(multi=> array(dimension=> array(array=>foo2))),
array multi=> array(dimensional=> array(array=>foo3))),
array(multi=> array(dimension=> (array=>foo4)))
array(multi=> array(dimensional=> array(array=>foo5)))
array(multi=> array(dimensional=> array(array=>foo6))),
array(multi=> => array(array=>foo7))),
数组(multi=>数组)),
array(multi=> array(dimensional=> array(array=>foo9)))
);
print_r(总结($ 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)


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 ) 

这篇关于动态关联数组 - 列表,计数,总和,最小值,最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆