在多维数组php中取消设置数组

时间:2014-12-01 23:00:11

标签: php multidimensional-array foreach

我有一个多维数组,如果他们有相同的division_id但是post_page_id不同,我会尝试加总观众总数,并返回一个数组,其中包含每个分区的总值,并取消其余部分。我尝试了双重foreach但它没有用。有帮助吗?感谢

Array
(
    [0] => Array
        (
            [id] => 1
            [post_page_id] => 22130
            [audience] => 276
            [type] => facebook
            [division_id] => 1
            [tw_audience] => 
        )

    [1] => Array
        (
            [id] => 14
            [post_page_id] => 22465
            [audience] => 6
            [type] => facebook
            [division_id] => 1
            [tw_audience] => 
        )

    [2] => Array
        (
            [id] => 2
            [post_page_id] => 22189
            [audience] => 175
            [type] => twitter
            [division_id] => 2
            [tw_audience] => 
         )
    [3] => Array
        (
            [id] => 1
            [post_page_id] => 23044
            [audience] => 180
            [type] => facebook
            [division_id] => 2
            [tw_audience] => 
        )
)

所以我希望输出像这样

Array
(
    [0] => Array
        (
            [id] => 1
            [post_page_id] => 22130, 22465
            [audience] => 282
            [type] => facebook
            [division_id] => 1
            [tw_audience] => 
        )

    [1] => Array
        (
            [id] => 2
            [post_page_id] => 22189, 23044
            [audience] => 180
            [type] => twitter+facebook
            [division_id] => 2
            [tw_audience] => 175
         )

)

2 个答案:

答案 0 :(得分:0)

最好的方法不是取消设置当前数组的元素,而是写入一个新的元素,然后合并到那个元素。在迭代过程中修改数组的结构通常是一个坏主意。

如果要按division_id合并,可以使用该值作为数组键写入新数组。然后检查它是否已经存在 - 如果不存在,则复制那里的行,如果是,则合并你想要合并的值。

假设您的原始数组位于名为$originalArray的变量中:

$newArray = array();
foreach ($originalArray as $row) {
    $divID = $row['division_id'];
    // Check if this division_id has occurred before
    if (!isset($newArray[$divID])) {
        // Add in the first row if not
        $newArray[$divID] = $row;
    } else {
        // Otherwise, merge it into existing row

        // Merge audience
        $newArray[$divID]['audience'] += $row['audience'];

        // Merge post IDs
        $newArray[$divID]['post_page_id'] .= ', ' . $row['post_page_id'];

        // Merge type - need to account for duplicates
        $typeVals = explode('+', $newArray[$divID]['type']);
        if (!in_array($row['type'], $typeVals)) {
            $newArray[$divID]['type'] .= '+' . $row['type'];
        }
    }

}

// Overwrite original array, and use array_values() to reset keys if needed
$originalArray = array_values($newArray);
unset($newArray, $typeVals);

您可能需要根据确切的数据类型优化合并方法,但您应该明白这一点。

答案 1 :(得分:0)

关联数组是你的朋友。

$result = array ();
foreach ($input as $inp)
{
    $idx = $inp ['division_id'];
    if  (isset ($result [$idx]))  //  New result, just copy it across
    {
        $result [$idx] = $inp;
        //  Since we are going to have multiple pages, turn the page_id into an array
        $result [$idx]['post_page_id'] = array ($inp ['post_page_id']);
    }
    else  //  Already exists - add the totals to the existing one
    {
        $result [$idx]['audience'] += $inp ['audience'];
        $result [$idx]['post_page_id'][] = $inp ['post_page_id'];
    }
}
//  Now, I assume you want to get rid of duplicates
foreach ($result as &$r)
{
    //  &$r means you are working with a pointer to the original, not a copy
    //  so changes are applied back to the original
    $r ['post_page_id'] = array_unique [$r ['post_page_id']];
    //  and if you really want the pages in comma-delimited format...
    $r ['post_page_id'] = implode (', ', $r ['post_page_id']);
}

您可能希望对post_page_id执行相同的操作,但使用'+'将其删除。