PHP基于搜索结果拆分数组

时间:2011-05-14 15:58:41

标签: php arrays search multidimensional-array

我有一个多维数组,由MySQL查询创建,它根据多个组和总和收集结果。阵列在下面。

我对每种“ad_type”,“click_status”和“link_status”变体的costtotal和hitcount感兴趣。

已知3种变量的可能值: 即

  • ad_type 0/1
  • click_status 2/3
  • link_status 1/2

我想根据每种组合的结果创建一个新数组。

我猜测搜索或拆分可以做到,但我没有太多运气。

我将如何做到这一点?

Array
(
    [0.261346210037681] => Array
        (
            [costtotal] => 0.0015
            [hitcount] => 1
            [ad_type] => 0
            [click_status] => 2
            [link_status] => 1
        )

    [0.190427019438173] => Array
        (
            [costtotal] => 0.001
            [hitcount] => 1
            [ad_type] => 0
            [click_status] => 3
            [link_status] => 1
        )

    [0.563596305962276] => Array
        (
            [costtotal] => 0.007
            [hitcount] => 5
            [ad_type] => 1
            [click_status] => 2
            [link_status] => 1
        )

    [0.893211513658251] => Array
        (
            [costtotal] => 0
            [hitcount] => 3
            [ad_type] => 1
            [click_status] => 2
            [link_status] => 2
        )

    [0.209184847035617] => Array
        (
            [costtotal] => 0.004
            [hitcount] => 2
            [ad_type] => 1
            [click_status] => 3
            [link_status] => 1
        )

    [0.73545002260753] => Array
        (
            [costtotal] => 0
            [hitcount] => 1
            [ad_type] => 1
            [click_status] => 3
            [link_status] => 2
        )

)

2 个答案:

答案 0 :(得分:1)

如果我完全理解你想要什么,那么这段代码应该让你满意:

function generateClickCounterInfo() {
    return array(
        'costTotal' => 0.0,
        'hitCount' => 0
    );
}

function generateLinkStatusStructure() {
    return array(
        1 => generateClickCounterInfo(),
        2 => generateClickCounterInfo()
    );
}

function generateClickStatusStructure() {
    return array(
        2 => generateLinkStatusStructure(),
        3 => generateLinkStatusStructure()
    );
}

function generateAdTypeArrayStructure() {
    return array(
        0 => generateClickStatusStructure(),
        1 => generateClickStatusStructure()
    );
}

function getClickCounterReport(array $data) {
    $result = generateAdTypeArrayStructure();

    foreach ($data as $key => $value) {
        $adType      = $value['ad_type'];
        $clickStatus = $value['click_status'];
        $linkStatus  = $value['link_status'];


        if (!isset($result[$adType])
            || !isset($result[$adType][$clickStatus])
            || !isset($result[$adType][$clickStatus][$linkStatus])) {
            throw new Exception(
                "Input data does not conform to expected format. " .
                "ad_type = {$adType}, click_status = {$clickStatus}, link_status = ${linkStatus}"
            );
        }

        $costTotal = $value['costtotal'];
        $hitCount  = $value['hitcount'];

        $result[$adType][$clickStatus][$linkStatus]['costTotal'] += $costTotal;
        $result[$adType][$clickStatus][$linkStatus]['hitCount']  += $hitCount;
    }

    return $result;
}

getClickCounterReport($data)(其中$data是您提供的数据)将生成以下数组:http://pastebin.ubuntu.com/607464/

P.S。了解缺点:

  • 没有OOP(但这些功能很容易转换为方法)
  • Magick数字(0,1,2,3等)

答案 1 :(得分:0)

不需要进行数组拆分。只需创建变量,这些变量将存储您要测量的每个排列的总数并迭代数组。根据您在ad_type,click_status和link_status中观察到的值添加适当的变量。