动态关联数组 - 列表,计数,总和,最小值,最大值

时间:2015-12-02 05:18:01

标签: php function dynamic multidimensional-array associative-array

我有一个大约有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'),array('list','list','list','count') );}

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

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

最终目标是具有以下功能:

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;
    }
}

感谢您提供有关如何进行间接参考的任何帮助。

JC

解决 设法解决此问题的最终版本如下所示,感谢用户:检查,以便让我找到正确的路径。

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;
}

这给出了以下结果:

/*
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
        )

)

1 个答案:

答案 0 :(得分:2)

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

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;
}

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

$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));

输出继电器

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