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


Unset array in multi dimensional array php

我有一个多维数组,如果他们具有相同的division_id但不同的post_page_id,我会尝试将受众的总和相加,并返回一个包含每个除法总值的数组并取消设置其余部分。我试过双倍,但没有用。有什么帮助吗?谢谢

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

关联数组是你的朋友。

$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相同的操作,但用"+"内爆它们。

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

如果要按 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);

您可能需要根据确切的数据类型优化合并方法,但您应该了解这个想法。