Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多维数组php中的Unset数组_Php_Multidimensional Array_Foreach - Fatal编程技术网

多维数组php中的Unset数组

多维数组php中的Unset数组,php,multidimensional-array,foreach,Php,Multidimensional Array,Foreach,我有一个多维数组,我尝试将观众总数相加,如果他们有相同的分区id但不同的post\u page\u id,则返回一个数组,其中包含每个分区的总值,并取消设置其余部分。我试过双foreach,但没用。有什么帮助吗?谢谢 Array ( [0] => Array ( [id] => 1 [post_page_id] => 22130 [audience] => 276

我有一个多维数组,我尝试将观众总数相加,如果他们有相同的分区id但不同的post\u page\u 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
         )

)

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

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

您可能需要根据确切的数据类型改进合并方法,但您应该会有这个想法。

关联数组是您的朋友

$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的源代码执行相同的操作,但使用“+”对其进行内爆。

鉴于这里的数据,您能否提供一个示例,说明您希望结果是什么样子?刚刚编辑,基本上,我希望合并数组,汇总受众,并取消合并的受众。谢谢小睡!尽管您的第五行可能是
if(!isset…
)。