我试过各种各样的解决方案,但似乎没有一个能满足我的需要——或者我不知道如何改变它们来解决我的特定问题。基本上,我从我的SQL服务器返回一堆行。查询如下所示:
$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);
然后使用以下命令将行放入数组中:
// Put results into an array
while( $row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC))
{
$results['results'][] = $row;
}
$results['results']当我搜索"tp-ac1750(删除了一些返回列以便于查看):
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
)
我希望数组是这样的:
Array (
[results] => Array (
[0] => Array (
[ItemLookupCode] => TP-AC1750
[ReorderNumber] => Array (
[0] => ARCHERC7
[1] => N82E16833704177
[2] => 7681617
)
)
)
)
I have try:
- array_unique
- array_unique_recursive
- array_walk_recursive
- array_merge_recursive
- 和更多(各种组合)
但是我似乎不能把它做好。这是我现在正在尝试的:
// 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;
}
是否有一种方法来完成这种类型的合并?是否有一种方法来合并这些重复的SQL查询期间?任何建议将不胜感激!提前感谢:)
Update (Full Array Being working With)
这是我需要使用这个合并的实际数组(我仍然想使用ItemLookupCode作为唯一的键,但合并所有其他兄弟键):
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
)
)
我能想到的最快的方法:
<?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);