如何在多维数组中组合数组值

时间:2016-11-24 07:04:00

标签: php arrays

Array
    (
        [0] => Array
            (
                [source_name] => src_email_24
                [suggested_by] => Sameer
                [email_id] => sameer.m@blueoceanmi.com
                [medium_name] => Email
            )
    [1] => Array
        (
            [source_name] => src_email_24
            [suggested_by] => Sameer
            [email_id] => sameer.m@blueoceanmi.com
            [medium_name] => Display
        )
    )

如果source_name和medium_name相同,那么我想要下面的输出 我如何将多维aaray合并为一个数组。任何帮助都会有很大的帮助。

 Array
        (
            [0] => Array
                (
                    [source_name] => src_email_24
                    [suggested_by] => Sameer 

                    [email_id] => sameer.m@blueoceanmi.com
                    [medium_name] => Email,Display
                )
        )

1 个答案:

答案 0 :(得分:1)

希望source_name和电子邮件地址改为source_name和medium_name;此外,您应该只有一条新数据集上的特定电子邮件记录

<?php

$originalDataSet = [
    [
        'source_name'   => 'src_email_24',
        'suggested_by'  => 'Sameer',
        'email_id'      => 'sameer.m@blueoceanmi.com',
        'medium_name'   => 'Email'
    ],
    [
        'source_name'   => 'src_email_24',
        'suggested_by'  => 'Sameer',
        'email_id'      => 'sameer.m@blueoceanmi.com',
        'medium_name'   => 'Display'
    ]
];
$processedDataSet = getProcessedDataFromOriginalDataSet($originalDataSet);


function getProcessedDataFromOriginalDataSet($originalDataSet = []) {
    $processedDataSet = [];
    foreach ($originalDataSet as $data) {
        if (isset($processedDataSet[$data['email_id']])) {
            if ($processedDataSet[$data['email_id']]['source_name'] == $data['source_name'] /* && other conditions*/) {
                //change only relevant values
            }
        } else {
            $processedDataSet[$data['email_id']] = $data;
        }

    }

    return $processedDataSet;
}

其他类似的东西

function getProcessedDataFromOriginal($originalDataSet = []) {
    $processedDataSet = [];
    foreach ($originalDataSet as $originalData) {
        $isAdded = 0;
        foreach ($processedDataSet as $processedData) {
            if ($processedData['source_name'] == $originalData['source_name'] /* && other conditions*/) {
                // make changes on processed data set
                $isAdded = 1;
                break;
            }
        }
        if  (!$isAdded) {
            $processedData[] = $originalData;
        }
    }

    return $processedDataSet;
}