为了账号安全,请及时绑定邮箱和手机立即绑定

删除 php 数组中的重复项

删除 php 数组中的重复项

PHP
杨魅力 2023-09-15 09:47:07
我的阵列:Array ( [0] => Array ( [month] => November [Average Purchase Price] => 2.52 [Total Purchase Gallons] => 84000 ) [1] => Array ( [month] => October [Average Purchase Price] => 2.615 [Total Purchase Gallons] => 63000 ) [2] => Array ( [month] => November [Average Customer Price] => 2.79 [Total Customer Gallons] => 25000 ) [3] => Array ( [month] => October [Average Customer Price] => 2.9050000000000002 [Total Customer Gallons] => 5500 ) )我希望能够回显 [月份] 并且不重复它,但仍将其他值与正确的月份相关联。为了达到这一点,我做了一个 array_merge 将它们放在一起,就像你看到的那样。它们分别看起来像这样:#1Array ( [0] => Array ( [month] => November [Average Purchase Price] => 2.52 [Total Purchase Gallons] => 84000 ) [1] => Array ( [month] => October [Average Purchase Price] => 2.615 [Total Purchase Gallons] => 63000 ) )#2Array ( [0] => Array ( [month] => November [Average Customer Price] => 2.79 [Total Customer Gallons] => 25000 ) [1] => Array ( [month] => October [Average Customer Price] => 2.9050000000000002 [Total Customer Gallons] => 5500 ) ) 我已经尝试过 array_unique 但不起作用。我正在使用 foreach 语句来回显这些值。谢谢你!SQL 查询:$sql = "SELECT month, AVG(price) AS 'Average Purchase Price', SUM(gallons) as 'Total Purchase Gallons' from purchase_contractsgroup BY month";$purch = mysqli_query($con, $sql) or die(mysqli_error($con));while ($rows = mysqli_fetch_assoc($purch)){     $purch_items[] = $rows;}$sql1 = "SELECT month, AVG(price) AS 'Average Customer Price', SUM(gallons) as 'Total Customer Gallons' from customer_contracts group BY month"; $cust = mysqli_query($con, $sql1) or die(mysqli_error($con)); while ($rows1 = mysqli_fetch_assoc($cust)) {    $cust_items[] = $rows1; }
查看完整描述

2 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

从原来的两个数组中,只需重新索引month并递归合并它们:


$result = array_merge_recursive(array_column($array1, null, 'month'),

                                array_column($array2, null, 'month'));

其产量:


Array

(

    [November] => Array

        (

            [month] => Array

                (

                    [0] => November

                    [1] => November

                )

            [Average Purchase Price] => 2.52

            [Total Purchase Gallons] => 84000

            [Average Customer Price] => 2.79

            [Total Customer Gallons] => 25000

        )

    [October] => Array

        (

            [month] => Array

                (

                    [0] => October

                    [1] => October

                )

            [Average Purchase Price] => 2.615

            [Total Purchase Gallons] => 63000

            [Average Customer Price] => 2.905

            [Total Customer Gallons] => 5500

        )

)

轻松访问一个月内的任何密钥:


echo $result['October']['Average Purchase Price'];

echo $result['October']['Average Customer Price'];

// etc...

或者循环:


foreach($result as $month => $values) {

    echo $month;

    echo $values['Average Purchase Price'];

    echo $values['Average Customer Price'];

    // etc...

}

但是,您在两个可以组合的查询中进行了编辑。这可能会起作用,或者会问另一个问题,毫无疑问有人可以给你一个查询:


SELECT month, AVG(price) AS 'Average Purchase Price', SUM(gallons) AS 'Total Purchase Gallons'

    FROM purchase_contracts GROUP BY month


UNION ALL


SELECT month, AVG(price) AS 'Average Customer Price', SUM(gallons) AS 'Total Customer Gallons'

    FROM customer_contracts GROUP BY month


查看完整回答
反对 回复 2023-09-15
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

这很简单的迭代:


<?php


$oldArr = [

    ['month' => 'November', 'Average Purchase Price' => 2.52, 'Total Purchase Gallons' => 84000],

    ['month' => 'October', 'Average Purchase Price' => 2.615, 'Total Purchase Gallons' => 63000],

    ['month' => 'November', 'Average Customer Price' => 2.79, 'Total Customer Gallons' => 25000],

    ['month' => 'October', 'Average Customer Price' => 2.9050000000000002, 'Total Customer Gallons' => 5500]

];


$newArr = [];

foreach ($oldArr as $item) {

    $month = $item['month'];

    if (!array_key_exists($month, $newArr)) {

        $newArr[$month] = $item;

    }

}


echo '<pre>';

print_r($newArr);


查看完整回答
反对 回复 2023-09-15
  • 2 回答
  • 0 关注
  • 96 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信