获取数组,并添加包含数组

时间:2017-02-14 22:30:20

标签: php arrays

我有一个二维数组。

$original_array = array(); 
// amount / funding_by / withdrawing_by / bank_name_1 / bank_name_2
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "ANZ");
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "BOA"); 
$original_array[] = array("100","Cash","Bank Deposit", "Chase", "BOA");
$original_array[] = array("100","Bank Deposit","Cash", "Chase", "BOA");
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "JP Morgan"); 

我想添加一个额外的字段(funding_by_to_withdrawing_by),该字段是通过使用funding_by和withdrawing_by字段形成的,并将"分隔为"。 IE浏览器。银行存款到银行存款

最后,我想压缩数组,检查金额,bank1_name和bank_2_name是否都与另一行匹配,如果是,则将原始行的funding_by_to_withdrawing_by字段附加到副本的funding_by_to_withdrawing_by字段行。

然后可以删除重复行,因此处理后的数组如下所示:

$processed_array = array(); 
// amount / funding_by / withdrawing_by / bank_name_1 / bank_name_2 / funding_by_to_withdrawing_by
$processed_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "ANZ", "Bank Deposit to Bank Deposit");
$processed_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "BOA", "Bank Deposit to Bank Deposit, Cash to Bank Deposit, Bank Deposit to Cash"); 
$processed_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "JP Morgan");

我已经尝试了这一点,但老实说我只是更加困惑,而且它不起作用:

$original_array = array(); 
// amount / funding_by / withdrawing_by / bank_name_1 / bank_name_2
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "ANZ");
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "BOA"); 
$original_array[] = array("100","Cash","Bank Deposit", "Chase", "BOA");
$original_array[] = array("100","Bank Deposit","Cash", "Chase", "BOA");
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "JP Morgan"); 

$processed_array = array(); 
foreach($original_array as $submitted_data) 
{ 
    foreach($processed_array as $verified_data) 
    {     
        if ($submitted_data[0]==$verified_data[0]) 
        { 
            $duplicate_found = true; 
            $listoffundingtowithdrawalmethods = "$fundingname to $withdrawalname<br>" . $submitted_data[1] . " to " . $submitted_data[2];
            array_push($verified_data, "$listoffundingtowithdrawalmethods");
            $processed_array[] = $verified_data; 
        } 
        else
        {
            $duplicate_found = ""; 
        }
    } 
    if ($duplicate_found != true)
    {
        $listoffundingtowithdrawalmethods = "$fundingname to $withdrawalname<br>";
        array_push($submitted_data, "$listoffundingtowithdrawalmethods");
        $processed_array[] = $submitted_data; 
    }
 } 

然而,它仍然不是很正确。我希望你们能够发挥作用。

谢谢你的时间!

马特

1 个答案:

答案 0 :(得分:0)

我会使用不同的方法。首先将原始数组转换为分层数组:

foreach ($original_array as $row) {
    $intermediate[$row[0]][$row[3]][$row[4]][] = "$row[1] to $row[2]";
}

这将形成如下数组:

[
    100 => [
        'Chase' => [
            'ANZ' => ['Bank Deposit to Bank Deposit'],
            'BOA' => [
                  'Bank Deposit to Bank Deposit',
                  'Cash to Bank Deposit',
                  'Bank Deposit to Cash'],
            'JP Morgan' => ['Bank Deposit to Bank Deposit']
        ]
    ]
];

然后迭代中间数组的三个级别以产生最终结果。

foreach ($intermediate as $amount => $bank1_names) {
    foreach ($bank1_names as $bank1_name => $bank2_names) {
        foreach ($bank2_names as $bank2_name => $transfers) {
            $result[] = [$amount, $bank1_name, $bank2_name, implode(", ", $transfers)];
        }
    }
}

这只需要相当于原始数据集的两次传递。结果将是这样的:

[
    [100, 'Chase', 'ANZ', 'Bank Deposit to Bank Deposit'],
    [100, 'Chase', 'BOA', 'Bank Deposit to Bank Deposit, Cash to Bank Deposit, Bank Deposit to Cash'],
    [100, 'Chase', 'JP Morgan', 'Bank Deposit to Bank Deposit']
]

它不包含"funding_by""withdrawing_by"列,但由于您将所有这些列合并到"funding_by_to_withdrawing_by"中,因此无论如何它们似乎都不再有意义。