【问题标题】:Combine intersecting keys and values in 2 arrays在 2 个数组中组合相交的键和值
【发布时间】:2015-10-07 18:03:38
【问题描述】:

我正在运行 php.net 上的所有数组函数,但无法弄清楚。

基本上我想取这两个数组:

Array
(
[0] => stdClass Object
    (
        [month] => October
        [year] => 2015
        [credit] => 1000.00
    )

[1] => stdClass Object
    (
        [month] => September
        [year] => 2015
        [credit] => 200.00
    )
)

Array
(

[0] => stdClass Object
    (
        [month] => October
        [year] => 2015
        [debit] => 2000.00
    )

[1] => stdClass Object
    (
        [month] => August
        [year] => 2015
        [debit] => 50.00
    )
)

...让输出看起来像这样:

Array
(

[0] => stdClass Object
    (
        [month] => October
        [year] => 2015
        [credit] => 1000.00
        [debit] => 2000.00
    )

[1] => stdClass Object
    (
        [month] => September
        [year] => 2015
        [credit] => 200.00
        [debit] => 0
    )

[2] => stdClass Object
    (
        [month] => August
        [year] => 2015
        [credit] => 0
        [debit] => 50.00
    )
)

我希望合并“月”和“年”并组合其他键,如果键不存在,则使用默认值。有什么指导吗?

【问题讨论】:

  • 据我所知,并没有真正的内置 PHP 函数,但是您应该能够遍历两个数组并使用月份+年份作为键构建一个新数组.这样组合起来应该很容易。
  • 我很想知道您将如何从您所谓的“所需”数据结构中访问数据。对我来说,您提出的数据结构在允许您访问其中的数据方面的用处有限。如果你总是要输出整个数组,那么一个对象数组可能没问题,但是如果你需要能够做一些事情,比如获取特定年份和/或月份的信息,这个数据结构会很差,每次想要获取其中的一个特定数据时,都需要迭代整个数组。
  • @MikeBrant 我也同意这一点。在回答是否只是向人们展示如何做他们正在尝试做的事情时,或者提出对我来说更好的事情时,我经常不确定。
  • @Don'tPanic 您应该随时在 cmets 中提出澄清问题。我也认为可以按要求回答问题,但如果您的评论得到答案,可能会导致您认为另一种解决方案是合适的,您始终可以编辑您的答案以显示替代方法。到达该页面的未来用户可以决定提出的问题是否符合他们的需求,或者确定的替代方法是否可以满足他们的需求。
  • @mike-brant 我分别处理两个数组,它们输入到显示利润的数据表中,但我现在有一个功能升级来添加日期范围过滤器,我有一个真正的问题句柄 2不同的数组并比较结果,然后将结果结合在一起显示为组合数据。我认为最好将所有内容放入一个数组中,这样我可以轻松控制后端输入这些数组的日期范围。我相信我的主要问题是两个初始数组都来自数据库中的同一个表,所以加入失败了?

标签: php arrays merge


【解决方案1】:

假设$debits$credits 是您的问题中显示的数组,我会这样处理:

首先循环贷方,将它们插入到新的“组合”数组中,并为借方添加默认值。

foreach ($credits as $credit) {
    $credit->debit = 0.00;   // provide a default value for debit
    $combined[$credit->year . $credit->month] = $credit;
}

然后循环借记。由于有可能已经存在来自学分的条目,因此需要对此进行检查。这部分应该更新从学分插入的现有值,如果没有现有值,则插入新值。

foreach ($debits as $debit) {
    if (isset($combined[$debit->year . $debit->month])) {
        // update the debit if the entry already exists
        $combined[$debit->year . $debit->month]->debit = $debit->debit;
    } else {
        // otherwise create a new entry with a default value for credit
        $debit->credit = 0.00;
        $combined[$debit->year . $debit->month] = $debit;
    }
}

// If you need the results to be numerically indexed, you can use array_values
$numerically_indexed = array_values($combined);

【讨论】:

  • 啊!!当我阅读您的解决方案时,我大约走了 40% 的路。你真的做到了。谢谢恐慌。
  • 我在最后加了这个来处理利润加成:
    // Create profit key and do the maths <br/> foreach ($combined as $value) { <br/> $value->profit = $value->credit - $value->debit; <br/> $value->profit = number_format($value->profit, 2, '.', ''); <br/> //print "<pre>";print_r($combined);exit(); <br/> }
猜你喜欢
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-04
相关资源
最近更新 更多