php 关联数组相减

发布于 2024-12-07 15:31:53 字数 942 浏览 0 评论 0原文

我有两个关联数组。我需要减去 ($price - $tax) 以获得 $total 价格:

 $price['lunch'] = array("food" => 10, "beer"=> 6, "wine" => 9);
 $price['dinner'] = array("food" => 15, "beer"=> 10, "wine" => 10);

 $tax['lunch'] = array("food" => 2, "beer"=> 3, "wine" => 2);
 $tax['dinner'] = array("food" => 4, "beer"=> 6, "wine" => 4);

所需的结果数组:

 $result['lunch'] = (['food'] => 8, ['beer'] =>3, ['wine'] => 7 )
 $result['dinner'] = (['food'] => 11, ['beer'] =>4, ['wine'] => 6   )

我正在尝试以下函数和 array_map 无济于事:

function minus($a, $b) {
    return $a - $b;
 }

      foreach ($price as  $value)
        {
            $big = $value;
            foreach($tax as $v) {
                $small = $v;
        $e[] = array_map("minus",$big, $small);
        }       
        }

上面我得到四个数组(尽管第一个和最后一个是正确的),所以它是不正确。感谢您提供任何信息!

I have two associative arrays. I need to subtract ($price - $tax) to get $total price:

 $price['lunch'] = array("food" => 10, "beer"=> 6, "wine" => 9);
 $price['dinner'] = array("food" => 15, "beer"=> 10, "wine" => 10);

 $tax['lunch'] = array("food" => 2, "beer"=> 3, "wine" => 2);
 $tax['dinner'] = array("food" => 4, "beer"=> 6, "wine" => 4);

Desired result array:

 $result['lunch'] = (['food'] => 8, ['beer'] =>3, ['wine'] => 7 )
 $result['dinner'] = (['food'] => 11, ['beer'] =>4, ['wine'] => 6   )

I'm trying following function and array_map to no avail:

function minus($a, $b) {
    return $a - $b;
 }

      foreach ($price as  $value)
        {
            $big = $value;
            foreach($tax as $v) {
                $small = $v;
        $e[] = array_map("minus",$big, $small);
        }       
        }

With above i get four arrays (first and last one is correct though) so it's not correct. Thanks for any info!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2024-12-14 15:31:53

您可以使用键而不是数组映射进行比较。使用双 foreach,您可以执行任意数量的餐食和食物类型:

foreach(array_keys($price) as $meal)
{
    foreach(array_keys($price[$meal]) as $type)
    {
        $e[$meal][$type] = $price[$meal][$type] - $tax[$meal][$type];
    }
}

请注意,此代码不会查找餐食或食物类型中缺少的项目...

You could compare using keys instead of array mapping. With a double foreach, you can do any number of meals and food types:

foreach(array_keys($price) as $meal)
{
    foreach(array_keys($price[$meal]) as $type)
    {
        $e[$meal][$type] = $price[$meal][$type] - $tax[$meal][$type];
    }
}

Note, this code doesn't look out for missing items in either the meals or the food types...

沩ん囻菔务 2024-12-14 15:31:53

如果$price$tax的结构始终相等,则可以使用以下代码:

<?php

$price['lunch'] = array("food" => 10, "beer"=> 6, "wine" => 9);
$price['dinner'] = array("food" => 15, "beer"=> 10, "wine" => 10);

$tax['lunch'] = array("food" => 2, "beer"=> 3, "wine" => 2);
$tax['dinner'] = array("food" => 4, "beer"=> 6, "wine" => 4);

$result = array();
foreach( $price as $what => $value)
{
    foreach( $value as $food => $foodPrice )
    {
        $result[$what][$food] = $foodPrice - $tax[$what][$food];
    }
}

结果:

print_r($result); 

Array
(
    [lunch] => Array
        (
            [food] => 8
            [beer] => 3
            [wine] => 7
        )

    [dinner] => Array
        (
            [food] => 11
            [beer] => 4
            [wine] => 6
        )

)

if the structure of $price and $tax are always equals, you can use the following code:

<?php

$price['lunch'] = array("food" => 10, "beer"=> 6, "wine" => 9);
$price['dinner'] = array("food" => 15, "beer"=> 10, "wine" => 10);

$tax['lunch'] = array("food" => 2, "beer"=> 3, "wine" => 2);
$tax['dinner'] = array("food" => 4, "beer"=> 6, "wine" => 4);

$result = array();
foreach( $price as $what => $value)
{
    foreach( $value as $food => $foodPrice )
    {
        $result[$what][$food] = $foodPrice - $tax[$what][$food];
    }
}

Result:

print_r($result); 

Array
(
    [lunch] => Array
        (
            [food] => 8
            [beer] => 3
            [wine] => 7
        )

    [dinner] => Array
        (
            [food] => 11
            [beer] => 4
            [wine] => 6
        )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文