开发者

PHP split array based on search results

I have a multidimensional array which is created by a MySQL query which collects results based on a number of groups and sums. The array is below.

I'm interested in the costtotal and hitcount for each type of 'ad_type', 'click_status' and 'link_status' variation.

The possible values of the 3 types of variable are known: i.e.

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

I would like to create a new array based on the results of each combination.

I'm guessing a search or split would do it but I'm not having much luck.

How would I go about doing this?

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
        )

)


If I fully understand what you want, then this code should satisfy you:

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

And than getClickCounterReport($data) (where $data is data provided by you) will produce following array: http://pastebin.ubuntu.com/607464/

P.S. Knowing disadvantages:

  • No OOP (but these functions will be easy to transform to methods)
  • Magick numbers (0, 1, 2, 3 etc)


No array splitting is necessary. Simply create the variables that will store the totals for each of the permutation you want to measure and iterate through your array. Add to appropriate variables based upon the value you observe in ad_type, click_status, and link_status.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜