在php中删除数组中的重复项
我的阵列:在php中删除数组中的重复项,php,sql,arrays,Php,Sql,Arrays,我的阵列: 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 ) [
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 ) )
我希望能够回显[month],而不是复制它,但仍然将其他值与正确的月份关联。为了达到这一点,我做了一个数组合并,把它们放在一起,就像你们看到的一样
它们分别如下所示:
#一,
#二,
我尝试了array_unique,但没有效果。我使用foreach语句来呼应这些值
谢谢大家!
SQL查询:
$sql = "SELECT month, AVG(price) AS 'Average Purchase Price', SUM(gallons) as 'Total Purchase Gallons' from purchase_contracts
group 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;
}
这很简单:
这是一个简单的迭代:
从原来的两个数组中,只需在月重新编制索引,然后递归合并它们:
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
但是,您在两个可以组合的查询中进行了编辑。这可能有用,也可能会问另一个问题,毫无疑问,有人会问你一个问题:
从最初的两个数组中,只需在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
但是,您在两个可以组合的查询中进行了编辑。这可能有用,也可能会问另一个问题,毫无疑问,有人会问你一个问题:
使用foreach
在月份名称为键时创建新数组。您是如何使用array\u unique的?这应该行。@不要惊慌--$combined=array\u merge($array1,$array2)$combined1=数组_唯一($combined);打印(合并1美元);结果:数组([0]=>数组([month]=>11月[平均采购价格]=>2.52[总采购加仑]=>84000))@biesior你能给我举个例子吗?我已经在使用一个foreach--foreach($composed as$purchase){#biesior我从两个SQL语句中得到了这个结果,并试图将它们组合在一起。当月份名称是一个键时,使用foreach
创建新数组。您是如何使用array_unique的?这应该可以工作。@不要惊慌--$composed=array_merge($array1,$array2)$combined1=array_unique($combined);print_r($combined1);Results:array([0]=>array([month]=>11月[Average Purchase Price]=>2.52[Total Purchase Gallons]=>84000))@biesior你能给我举个例子吗?我已经在使用foreach--foreach($combined as Purchase){#biesior我从两条SQL语句中得到了这个结果,并试图将它们组合在一起。非常感谢,这很好。我发布了SQL,但这确实有效。非常感谢。这在一个查询中肯定是可行的。我发布了一个未经测试的示例。非常感谢,这很好。我发布了SQL,但这确实有效。Greatl我很感激。这在一个查询中肯定是可行的。我发布了一个未经测试的示例。