如何根据一个键的值合并阵列中的所有重复项?

时间:2015-09-16 20:41:15

标签: php sql arrays tsql php-5.6

我尝试了各种各样的解决方案,似乎没有人能做我需要的 - 或者我不理解如何改变它们以解决我的特定问题。基本上,我从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;
}
当我搜索" 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

      )

    )

  )

)

我试过了:

  • 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查询期间合并这些重复项?任何意见,将不胜感激!在此先感谢:)

更新(正在使用的完整数组)

这是我需要使用此合并的实际数组(我仍然希望使用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

  )

)

1 个答案:

答案 0 :(得分:1)

我能想到的最快方式:

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