【问题标题】:Dynamic associative Array - list, count, sum, min, max动态关联数组 - 列表、计数、总和、最小值、最大值
【发布时间】:2016-03-06 06:19:17
【问题描述】:

我有一个包含大约 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
        )

)

【问题讨论】:

  • 请不要在您的问题标题中放置任何标记/标签。接受答案是表示您对收到的答案感到满意的方式。此外,您不应该在您的问题中发布您的解决方案。如果它与已发布的其他答案不同,请将其作为答案发布。

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


【解决方案1】:

或者,如果我假设 $field_array 包含从根到子键的顺序键字段,您可以在 $data_array 循环中循环您的 $field_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 ) 

【讨论】:

    猜你喜欢
    • 2018-10-30
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多