我该如何合并所有重复阵列基于一个键的值? [英] How Can I Merge All Duplicates In Array Based On One Key's Value?
问题描述
我已经尝试了各种解决方案,并没有似乎做什么,我需要 - 或者我不知道如何变身他们解决我的具体问题。基本上,我从我的SQL服务器返回一串行。查询看起来是这样的:
$ PARAMS =阵列(安培; $搜索,和放大器; $搜索,和放大器; $搜索,和放大器; $搜索,和放大器; $搜索,和放大器; $搜索,和放大器; $搜索,和放大器; $搜索);$ TSQL =SELECT Item.ID,Item.ItemLookup code,nitroasl_pamtable.ManufacturerPartNumber,SupplierList.ReorderNumber,Item.Notes,
Item.Description,Item.ExtendedDescription,Item.Quantity,nitroasl_pamtable.SpoofStock,Item.Price,
nitroasl_pamtable.PAM_Keywords,Item.PictureName
从项目
INNER JOIN nitroasl_pamtable ON Item.ID = nitroasl_pamtable.ItemID
INNER JOIN SupplierList ON Item.ID = SupplierList.ItemID
WHERE(Item.ItemLookup code LIKE'%'+'%'和价格> 0.00的WebItem = 1)
OR(Item.ID LIKE'%'+'%'和价格> 0.00的WebItem = 1)
OR(nitroasl_pamtable.ManufacturerPartNumber LIKE'%'+'%'和价格> 0.00的WebItem = 1)
OR(SupplierList.ReorderNumber LIKE'%'+'%'和价格> 0.00的WebItem = 1)
OR(Item.Notes LIKE'%'+'%'和价格> 0.00的WebItem = 1)
OR(Item.Description LIKE'%'+'%'和价格> 0.00的WebItem = 1)
OR(Item.ExtendedDescription LIKE'%'+'%'和价格> 0.00的WebItem = 1)
OR(nitroasl_pamtable.PAM_Keywords LIKE'%'+'%'和价格> 0.00的WebItem = 1);//使我们能够确定返回的行数
$的CursorType =阵列('滚动'=> SQLSRV_CURSOR_KEYSET);$ =的getProducts sqlsrv_query($康恩,$ TSQL,$参数,可以$的CursorType);
然后我用下面把行到一个数组:
//把结果到一个数组
而($行= sqlsrv_fetch_array($的getProducts,SQLSRV_FETCH_ASSOC))
{
$结果['结果'] [] = $行;
}
$结果['结果'] 是这样的,当我搜索 TP-ac1750 的(去掉了一些返回以方便查看列)
阵列( [结果] =>阵列( [0] =>阵列( [ItemLookup code => TP-AC1750 [ReorderNumber] => ARCHERC7 ) [1] =>阵列( [ItemLookup code => TP-AC1750 [ReorderNumber] => N82E16833704177 ) [2] =>阵列( [ItemLookup code => TP-AC1750 [ReorderNumber] => 7681617 ) [3] =>阵列( [ItemLookup code => TP-AC1750 [ReorderNumber] => ARCHERC7 ) ) [关键词] => TP-ac1750)
我想数组是这样的:
阵列( [结果] =>阵列( [0] =>阵列( [ItemLookup code => TP-AC1750 [ReorderNumber] =>阵列( [0] => ARCHERC7 [1] => N82E16833704177 [2] => 7681617 ) ) ))
我曾尝试:
- array_unique
- array_unique_recursive
- array_walk_recursive
- array_merge_recursive
- 多(各种组合)
但我似乎无法得到它的权利。这里就是我现在正在努力:
//删除重复
$结果['结果'] = merge_duplicates($结果['结果']);// ***********************************************
//合并重复阵列和它们的值
// ***********************************************
功能merge_duplicates($数组)
{ //建立array_unique临时数组
$ TMP =阵列();
的foreach($数组$关键=> $值){
$ TMP [$关键] = $价值;
} //查找临时数组复制
$ TMP = array_unique($ TMP,SORT_REGULAR); //取下原数组复制
的foreach($数组$关键=> $值){ 如果(!array_key_exists($键,$ TMP)){ 未设置($数组[$关键]); } } 返回$阵列;}
有没有办法来完成这种类型合并?有没有办法在SQL查询中合并这些重复?任何意见将是AP preciated!在此先感谢:)
更新(全阵列正在处理含)
下面是实际的数组我需要使用此合并上(我还是想用ItemLookup code作为唯一键,但合并所有其他的兄弟姐妹键):
阵列( [0] =>阵列( [ID] => 8265
[ItemLookup code => TP-AC1750
[ManufacturerPartNumber] =>射手C7
[ReorderNumber] => ARCHERC7
[注意事项] => TP-LINK射手C7 AC1750 Routr
[说明] => TP-LINK射手C7 AC1750 Routr
[ExtendedDescription] => TP-Link的射手C7的Wireless-AC1750双频千兆路由器
[数量] => 0
[SpoofStock] =>
[参考价格] => 129.9500
[PAM_Keywords] =>
[PictureName] => TP-ac1750.jpg ) [1] =>阵列( [ID] => 8265
[ItemLookup code => TP-AC1750
[ManufacturerPartNumber] =>射手C7
[ReorderNumber] => N82E16833704177
[注意事项] => TP-LINK射手C7 AC1750 Routr
[说明] => TP-LINK射手C7 AC1750 Routr
[ExtendedDescription] => TP-Link的射手C7的Wireless-AC1750双频千兆路由器
[数量] => 0
[SpoofStock] =>
[参考价格] => 129.9500
[PAM_Keywords] =>
[PictureName] => TP-ac1750.jpg ) [2] =>阵列( [ID] => 8265
[ItemLookup code => TP-AC1750
[ManufacturerPartNumber] =>射手C7
[ReorderNumber] => 7681617
[注意事项] => TP-LINK射手C7 AC1750 Routr
[说明] => TP-LINK射手C7 AC1750 Routr
[ExtendedDescription] => TP-Link的射手C7的Wireless-AC1750双频千兆路由器
[数量] => 0
[SpoofStock] =>
[参考价格] => 129.9500
[PAM_Keywords] =>
[PictureName] => TP-ac1750.jpg ) [3] =>阵列( [ID] => 8265
[ItemLookup code => TP-AC1750
[ManufacturerPartNumber] =>射手C7
[ReorderNumber] => ARCHERC7
[注意事项] => TP-LINK射手C7 AC1750 Routr
[说明] => TP-LINK射手C7 AC1750 Routr
[ExtendedDescription] => TP-Link的射手C7的Wireless-AC1750双频千兆路由器
[数量] => 0
[SpoofStock] =>
[参考价格] => 129.9500
[PAM_Keywords] =>
[PictureName] => TP-ac1750.jpg ))
我能想到的最快方式:
< PHP
$结果=阵列(
阵列('ItemLookup code'=>'名称1','ReorderNumber'=> 1)
阵列('ItemLookup code'=>'名称1','ReorderNumber'=> 2),
阵列('ItemLookup code'=>'名称1','ReorderNumber'=> 3)
阵列('ItemLookup code'=>'名称1','ReorderNumber'=> 2),
阵列('ItemLookup code'=>'名称2','ReorderNumber'=> 1)
阵列('ItemLookup code'=>'名称2','ReorderNumber'=> 1)
);功能组($为主,$项目){
如果(!使用isset($主[$项目['ItemLookup code'])){
$主[$项目['ItemLookup code'] =阵列('ReorderNumber'=>阵());
}
如果(!in_array($项目['ReorderNumber'],$主[$项目['ItemLookup code'] ['ReorderNumber'])){
$主[$项目['ItemLookup code'] ['ReorderNumber'] [] = $项目['ReorderNumber'];
}
返回$为主;
}$ formatted_result =阵列();
的foreach(array_reduce($结果,集团)为$ NAME => $项){
$ formatted_result [] =数组(
ItemLookup code'=> $名称,
ReorderNumber'=> $项目
);
}
的print_r($ formatted_result);
I have tried all kinds of solutions, and none seem to do what I need- or I don't understand how to morph them to solve my particular problem. Basically, I am returning a bunch of rows from my SQL server. The query looks like this:
$params = array(&$search, &$search, &$search, &$search, &$search, &$search, &$search, &$search);
$tsql = "SELECT Item.ID, Item.ItemLookupCode, nitroasl_pamtable.ManufacturerPartNumber, SupplierList.ReorderNumber, Item.Notes,
Item.Description, Item.ExtendedDescription, Item.Quantity, nitroasl_pamtable.SpoofStock, Item.Price,
nitroasl_pamtable.PAM_Keywords, Item.PictureName
FROM Item
INNER JOIN nitroasl_pamtable ON Item.ID = nitroasl_pamtable.ItemID
INNER JOIN SupplierList ON Item.ID = SupplierList.ItemID
WHERE (Item.ItemLookupCode LIKE '%' + ? + '%' AND Price > 0.00 AND WebItem = 1)
OR (Item.ID LIKE '%' + ? + '%' AND Price > 0.00 AND WebItem = 1)
OR (nitroasl_pamtable.ManufacturerPartNumber LIKE '%' + ? + '%' AND Price > 0.00 AND WebItem = 1)
OR (SupplierList.ReorderNumber LIKE '%' + ? + '%' AND Price > 0.00 AND WebItem = 1)
OR (Item.Notes LIKE '%' + ? + '%' AND Price > 0.00 AND WebItem = 1)
OR (Item.Description LIKE '%' + ? + '%' AND Price > 0.00 AND WebItem = 1)
OR (Item.ExtendedDescription LIKE '%' + ? + '%' AND Price > 0.00 AND WebItem = 1)
OR (nitroasl_pamtable.PAM_Keywords LIKE '%' + ? + '%' AND Price > 0.00 AND WebItem = 1)";
// Allows us to determine the number of rows returned
$cursorType = array('Scrollable' => SQLSRV_CURSOR_KEYSET);
$getProducts = sqlsrv_query($conn, $tsql, $params, $cursorType);
I then use the following to put the rows into an array:
// Put results into an array
while( $row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC))
{
$results['results'][] = $row;
}
$results['results'] looks like this when I search "tp-ac1750 (Removed some returned columns for easier viewing):
Array (
[results] => Array (
[0] => Array (
[ItemLookupCode] => TP-AC1750
[ReorderNumber] => ARCHERC7
)
[1] => Array (
[ItemLookupCode] => TP-AC1750
[ReorderNumber] => N82E16833704177
)
[2] => Array (
[ItemLookupCode] => TP-AC1750
[ReorderNumber] => 7681617
)
[3] => Array (
[ItemLookupCode] => TP-AC1750
[ReorderNumber] => ARCHERC7
)
)
[keywords] => tp-ac1750
)
I would like the array to look like this:
Array (
[results] => Array (
[0] => Array (
[ItemLookupCode] => TP-AC1750
[ReorderNumber] => Array (
[0] => ARCHERC7
[1] => N82E16833704177
[2] => 7681617
)
)
)
)
I have tried:
- array_unique
- array_unique_recursive
- array_walk_recursive
- array_merge_recursive
- And more (various combinations)
But I cannot seem to get it right. Here is what I am trying now:
// Remove duplicates
$results['results'] = merge_duplicates( $results['results'] );
//***********************************************
// Merge duplicate arrays and their values
//***********************************************
function merge_duplicates( $array )
{
// Build temporary array for array_unique
$tmp = array();
foreach( $array as $key => $value ) {
$tmp[ $key ] = $value;
}
// Find duplicates in temporary array
$tmp = array_unique( $tmp, SORT_REGULAR );
// Remove duplicates from original array
foreach( $array as $key => $value ) {
if ( !array_key_exists( $key, $tmp ) ) {
unset( $array[ $key ] );
}
}
return $array;
}
Is there a way to accomplish this type of merge? Is there a way to merge these duplicates during the SQL query? Any advice would be appreciated! Thanks in advance :)
Update (Full Array Being Worked With)
Here is the actual array I need to use this merge on (I still want to use ItemLookupCode as the unique key, but merge all the other sibling keys):
Array (
[0] => Array (
[ID] => 8265
[ItemLookupCode] => TP-AC1750
[ManufacturerPartNumber] => Archer C7
[ReorderNumber] => ARCHERC7
[Notes] => TP-LINK Archer C7 AC1750 Routr
[Description] => TP-LINK Archer C7 AC1750 Routr
[ExtendedDescription] => TP-Link Archer C7 Wireless-AC1750 Dual-Band Gigabit Router
[Quantity] => 0
[SpoofStock] =>
[Price] => 129.9500
[PAM_Keywords] =>
[PictureName] => tp-ac1750.jpg
)
[1] => Array (
[ID] => 8265
[ItemLookupCode] => TP-AC1750
[ManufacturerPartNumber] => Archer C7
[ReorderNumber] => N82E16833704177
[Notes] => TP-LINK Archer C7 AC1750 Routr
[Description] => TP-LINK Archer C7 AC1750 Routr
[ExtendedDescription] => TP-Link Archer C7 Wireless-AC1750 Dual-Band Gigabit Router
[Quantity] => 0
[SpoofStock] =>
[Price] => 129.9500
[PAM_Keywords] =>
[PictureName] => tp-ac1750.jpg
)
[2] => Array (
[ID] => 8265
[ItemLookupCode] => TP-AC1750
[ManufacturerPartNumber] => Archer C7
[ReorderNumber] => 7681617
[Notes] => TP-LINK Archer C7 AC1750 Routr
[Description] => TP-LINK Archer C7 AC1750 Routr
[ExtendedDescription] => TP-Link Archer C7 Wireless-AC1750 Dual-Band Gigabit Router
[Quantity] => 0
[SpoofStock] =>
[Price] => 129.9500
[PAM_Keywords] =>
[PictureName] => tp-ac1750.jpg
)
[3] => Array (
[ID] => 8265
[ItemLookupCode] => TP-AC1750
[ManufacturerPartNumber] => Archer C7
[ReorderNumber] => ARCHERC7
[Notes] => TP-LINK Archer C7 AC1750 Routr
[Description] => TP-LINK Archer C7 AC1750 Routr
[ExtendedDescription] => TP-Link Archer C7 Wireless-AC1750 Dual-Band Gigabit Router
[Quantity] => 0
[SpoofStock] =>
[Price] => 129.9500
[PAM_Keywords] =>
[PictureName] => tp-ac1750.jpg
)
)
Fastest way I can think of:
<?php
$results = array(
array('ItemLookupCode' => 'name1', 'ReorderNumber' => 1),
array('ItemLookupCode' => 'name1', 'ReorderNumber' => 2),
array('ItemLookupCode' => 'name1', 'ReorderNumber' => 3),
array('ItemLookupCode' => 'name1', 'ReorderNumber' => 2),
array('ItemLookupCode' => 'name2', 'ReorderNumber' => 1),
array('ItemLookupCode' => 'name2', 'ReorderNumber' => 1),
);
function group($main, $item) {
if(!isset($main[$item['ItemLookupCode']])) {
$main[$item['ItemLookupCode']] = array('ReorderNumber' => array());
}
if(!in_array($item['ReorderNumber'], $main[$item['ItemLookupCode']]['ReorderNumber'])) {
$main[$item['ItemLookupCode']]['ReorderNumber'][] = $item['ReorderNumber'];
}
return $main;
}
$formatted_result = array();
foreach(array_reduce($results, "group") as $name => $item) {
$formatted_result[] = array(
'ItemLookupCode' => $name,
'ReorderNumber' => $item
);
}
print_r($formatted_result);
这篇关于我该如何合并所有重复阵列基于一个键的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!