1

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

  )

)
4
  • This isn't really what you're asking for, but it might be useful. geneticcoder.blogspot.com/2015/05/… Commented Sep 16, 2015 at 20:52
  • There are some possibilities in that article. I will experiment and see what happens :) There is kind of a reoccurring theme with these methods, which I don't know how to do... Many of these functions like array_merge_recursive want two arrays passed to the function for comparison... I need to compare many arrays (not just two)... Commented Sep 16, 2015 at 21:08
  • You're still INNER JOINing and returning (potentially) many rows for each Item in a single result set. This is not what I recommended. Commented Sep 22, 2015 at 21:43
  • This question was asked before we began our discussion in the other thread :) So this was the initial query I had... Commented Sep 22, 2015 at 22:29

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

This actually puts the combined results into an array named ReorderNumber inside of a parent array named ReorderNumber. Comically, I cannot figure out how to bump the values from the child up a level into the parent...
Array ( [0] => Array ( [ID] => 8265 [ReorderNumber] => Array ( [ReorderNumber] => Array ( [0] => N82E16833704177 [1] => 7681617 [2] => ARCHERC7 [3] => ARCHERC7 ) ) ) ) I changed ItemLookupCode to ID btw.
How could I adjust this script so that there is not multiple levels of ReorderNumber?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.