0

I have a dilemma. Because I'm using Amazon's MWS, I have to get the order of the array exactly as Amazon would before I hash the contents otherwise the hash that I make and the hash Amazon makes won't match and Amazon won't accept my feed.

While I know of PHP's usort I've never used it, so I'm not sure quite how to create a function that does what I need.

Here's the array:

Array (
    [0] => AWSAccessKeyId=XXXXXXXX
    [1] => Action=ListInventorySupply
    [2] => MarketplaceId=XXXXXXX
    [3] => ResponseGroup=Basic 
    [4] => SellerId=XXXXXX
    [5] => SellerSkus.member.10=SKU10
    [6] => SellerSkus.member.11=SKU11
    [7] => SellerSkus.member.1=SKU1
    [8] => SellerSkus.member.2=SKU2
    [9] => SellerSkus.member.3=SKU3
    [10] => SellerSkus.member.4=SKU4
    [11] => SellerSkus.member.5=SKU5
    [12] => SellerSkus.member.6=SKU6
    [13] => SellerSkus.member.7=SKU7
    [14] => SellerSkus.member.8=SKU8
    [15] => SellerSkus.member.9=SKU9
    [16] => SignatureMethod=HmacSHA256 
    [17] => SignatureVersion=2 
    [18] => Timestamp=2016-04-06T22%3A26%3A41Z 
    [19] => Version=2010-10-01
)

The problem is that Amazon's sort is just slightly different:

Array (
    [0] => AWSAccessKeyId=XXXXXXXX
    [1] => Action=ListInventorySupply
    [2] => MarketplaceId=XXXXXXX
    [3] => ResponseGroup=Basic 
    [4] => SellerId=XXXXXX
    [5] => SellerSkus.member.1=SKU1
    [6] => SellerSkus.member.10=SKU10
    [7] => SellerSkus.member.11=SKU11
    [8] => SellerSkus.member.2=SKU2
    [9] => SellerSkus.member.3=SKU3
    [10] => SellerSkus.member.4=SKU4
    [11] => SellerSkus.member.5=SKU5
    [12] => SellerSkus.member.6=SKU6
    [13] => SellerSkus.member.7=SKU7
    [14] => SellerSkus.member.8=SKU8
    [15] => SellerSkus.member.9=SKU9
    [16] => SignatureMethod=HmacSHA256 
    [17] => SignatureVersion=2 
    [18] => Timestamp=2016-04-06T22%3A26%3A41Z 
    [19] => Version=2010-10-01
)

You'll see that the keys for the first three SellerSkus.member.{#} elements are different. I need to be able to keep the remaining sort the same, except that when there are 10 or more SKU's it needs to sort like Amazon's sort.

I've read the PHP doc for usort, but I'm still not getting anything that works above 10 items. It seems like I have to keep track of the SellerSkus.member.{#} number, in order to sort them correctly, but I haven't the foggiest idea of how to do that with usort. Any guidance in the right direction would be awesome! Thanks.

3
  • 1
    Take a look at natsort, it finds the numeric part of the string and sorts it numerically. Commented Apr 6, 2016 at 22:54
  • @Barmar Thanks. natsort takes those elements SellerSkus.member.10 & SellerSkus.member.11 and places them behind the element SellerSkus.member.9. What's strange is that on the natsort page, it shows a "Standard sorting" section that sorts the values just as I need it, however, that's not how sort or even asort is actually sorting the array. Commented Apr 6, 2016 at 22:57
  • Sorry, I misread, I thought you wanted them in numeric order. Commented Apr 6, 2016 at 23:00

1 Answer 1

2

It looks like you want an ordinary lexicographic sort, but only using the part of the string before = when comparing. So your comparison function should find the =, get the substring before that, and compare them.

usort($array, function($a, $b) {
    $a = substr($a, 0, strpos($a, '='));
    $b = substr($b, 0, strpos($b, '='));
    return ($a < $b) ? -1 : 1;
});
Sign up to request clarification or add additional context in comments.

1 Comment

First of all, thank you! Second of all, I had to read your answer three times for me to figure out what 'lexographic' meant! Hahaha. I appreciate it!

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.