第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

刪除 php 數(shù)組中的重復(fù)項(xiàng)

刪除 php 數(shù)組中的重復(fù)項(xiàng)

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 ) )我希望能夠回顯 [月份] 并且不重復(fù)它,但仍將其他值與正確的月份相關(guān)聯(lián)。為了達(dá)到這一點(diǎn),我做了一個(gè) 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 ) ) 我已經(jīng)嘗試過 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貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊

從原來的兩個(gè)數(shù)組中,只需重新索引month并遞歸合并它們:


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

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

其產(chǎn)量:


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

        )

)

輕松訪問一個(gè)月內(nèi)的任何密鑰:


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

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

// etc...

或者循環(huán):


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

    echo $month;

    echo $values['Average Purchase Price'];

    echo $values['Average Customer Price'];

    // etc...

}

但是,您在兩個(gè)可以組合的查詢中進(jìn)行了編輯。這可能會(huì)起作用,或者會(huì)問另一個(gè)問題,毫無疑問有人可以給你一個(gè)查詢:


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


查看完整回答
反對(duì) 回復(fù) 2023-09-15
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

這很簡單的迭代:


<?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);


查看完整回答
反對(duì) 回復(fù) 2023-09-15
  • 2 回答
  • 0 關(guān)注
  • 140 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)