我是PHP编程新手,我正在尝试为Amazon REST请求创建签名请求。为了正确签署请求,我必须按字节值(不按字母顺序)对参数/值对进行排序。他们提供了一个样本集作为例子:
Service=AWSECommerceService
AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE
AssociateTag=mytag-20
Operation=ItemLookup
ItemId=0679722769
ResponseGroup=Images%2CItemAttributes%2COffers%2CReviews
Version=2013-08-01
Timestamp=2014-08-18T12%3A00%3A00Z
排序值:
AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE
AssociateTag=mytag-20
ItemId=0679722769
Operation=ItemLookup
ResponseGroup=Images%2CItemAttributes%2COffers%2CReviews
Service=AWSECommerceService
Timestamp=2014-08-18T12%3A00%3A00Z
Version=2013-08-01
所以,我创建了一个名为$parameters
的关联数组,它以这种语法保存所有这些值:
$parameters = array ( "Service=" => "AWSECommerceService",
"AWSAccessKeyId=" => "AKIAIOSFODNN7EXAMPLE",
"AssociateTag=" => "mytag-20",
"Operation=" => "ItemLookup"
);
是否有方法对连接的各个键/值对的组合值进行排序?或者我是否必须将它们组合成一个临时数组,对它们进行排序,然后将它们放回到关联数组中?
非常感谢任何指导。
答案 0 :(得分:1)
我不知道为什么JYoThl删除了他的anwser,ksort将完成这项工作:
$parameters = array(
"Service=" => "AWSECommerceService",
"AWSAccessKeyId=" => "AKIAIOSFODNN7EXAMPLE",
"AssociateTag=" => "mytag-20",
"Operation=" => "ItemLookup",
"ItemId=" => "0679722769",
"ResponseGroup=" => "Images%2CItemAttributes%2COffers%2CReviews",
"Version=" => "2013-08-01",
"Timestamp=" => "2014-08-18T12%3A00%3A00Z"
);
ksort($parameters);
print_r($parameters);
输出:
Array
(
[AWSAccessKeyId=] => AKIAIOSFODNN7EXAMPLE
[AssociateTag=] => mytag-20
[ItemId=] => 0679722769
[Operation=] => ItemLookup
[ResponseGroup=] => Images%2CItemAttributes%2COffers%2CReviews
[Service=] => AWSECommerceService
[Timestamp=] => 2014-08-18T12%3A00%3A00Z
[Version=] => 2013-08-01
)
答案 1 :(得分:0)
使用ksort()根据键:
按升序对关联数组进行排序SKLabelNode