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