如何在PHP中按值对数组排序?
我知道以前有人问过这个问题,我甚至试着在这个问题上这样做: 但在我的情况下,它不起作用。如果有人能帮助我,我将非常感激 我的代码:如何在PHP中按值对数组排序?,php,Php,我知道以前有人问过这个问题,我甚至试着在这个问题上这样做: 但在我的情况下,它不起作用。如果有人能帮助我,我将非常感激 我的代码: $product_var_tpl = array( 'name' => $product['name'].(isset($product['attributes']) ? ' - '.$product['attributes'] : ''), 'unit_pri
$product_var_tpl = array(
'name' => $product['name'].(isset($product['attributes']) ? ' - '.$product['attributes'] : ''),
'unit_price' => Tools::displayPrice($product_price, $this->context->currency, false),
'price' => Tools::displayPrice($product_price * $product['quantity'], $this->context->currency, false),
'quantity' => $product['quantity'],
'reference' => $seller_name,
'customization' => array()
);
我想按字母顺序按“reference”对这个数组进行排序
我试过这个:
usort($product_var_tpl, function($a, $b) {
return $a['reference'] - $b['reference'];
});
但结果为null或空
默认情况下,输出为:
Referance | Product name | Unite price | Qty | Price
testshop2 | pere | 42,00 | 0.5 | 21,00
testshoptwo | portocale | 21,00 | 1 | 21,00
irinatestshop | qiwi | 34,00 | 0.5 | 17,00
irinatestshop | Banane | 12,00 | 1 | 12,00
如果我使用“usort”,我只得到4个空行
这是完整的代码:
// Construct order detail table for the email
$products_list = '';
$virtual_product = true;
$product_var_tpl_list = array();
foreach ($order->product_list as $product) {
$price = Product::getPriceStatic((int)$product['id_product'], false, ($product['id_product_attribute'] ? (int)$product['id_product_attribute'] : null), 6, null, false, true, $product['cart_quantity'], false, (int)$order->id_customer, (int)$order->id_cart, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$price_wt = Product::getPriceStatic((int)$product['id_product'], true, ($product['id_product_attribute'] ? (int)$product['id_product_attribute'] : null), 2, null, false, true, $product['cart_quantity'], false, (int)$order->id_customer, (int)$order->id_cart, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$product_price = Product::getTaxCalculationMethod() == PS_TAX_EXC ? Tools::ps_round($price, 2) : $price_wt;
//aici se pune date despre vinzaor
$idProduct = (int)$product['id_product'];
$SellerInfoOverride = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'wk_mp_seller_product` WHERE `id_ps_product` = '.(int) $idProduct);
//print_r ($SellerInfoOverride);
foreach ($SellerInfoOverride as $key => $value) {
$sellerid = $value['id_seller'];
//$seller = $mpSeller->getSeller($value['id_seller'], $this->context->language->id);
$SellerInfoDetails = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'wk_mp_seller` WHERE `id_seller` = '.(int) $sellerid);
foreach ($SellerInfoDetails as $key => $value) {
$seller_name = $value['shop_name_unique'];
}
}
$product_var_tpl = array(
'name' => $product['name'].(isset($product['attributes']) ? ' - '.$product['attributes'] : ''),
'unit_price' => Tools::displayPrice($product_price, $this->context->currency, false),
'price' => Tools::displayPrice($product_price * $product['quantity'], $this->context->currency, false),
'quantity' => $product['quantity'],
'reference' => $seller_name,
'customization' => array()
);
usort($product_var_tpl, function($a, $b) {
return $a['reference'] - $b['reference'];
});
$customized_datas = Product::getAllCustomizedDatas((int)$order->id_cart);
if (isset($customized_datas[$product['id_product']][$product['id_product_attribute']])) {
$product_var_tpl['customization'] = array();
foreach ($customized_datas[$product['id_product']][$product['id_product_attribute']][$order->id_address_delivery] as $customization) {
$customization_text = '';
if (isset($customization['datas'][Product::CUSTOMIZE_TEXTFIELD])) {
foreach ($customization['datas'][Product::CUSTOMIZE_TEXTFIELD] as $text) {
$customization_text .= $text['name'].': '.$text['value'].'<br />';
}
}
if (isset($customization['datas'][Product::CUSTOMIZE_FILE])) {
$customization_text .= sprintf(Tools::displayError('%d image(s)'), count($customization['datas'][Product::CUSTOMIZE_FILE])).'<br />';
}
$customization_quantity = (int)$product['customization_quantity'];
$product_var_tpl['customization'][] = array(
'customization_text' => $customization_text,
'customization_quantity' => $customization_quantity,
'quantity' => Tools::displayPrice($customization_quantity * $product_price, $this->context->currency, false)
);
}
}
$product_var_tpl_list[] = $product_var_tpl;
// Check if is not a virutal product for the displaying of shipping
if (!$product['is_virtual']) {
$virtual_product &= false;
}
} // end foreach ($products)
//为电子邮件构造订单详细信息表
$products_list='';
$virtual_product=true;
$product_var_tpl_list=array();
foreach($order->product\u作为$product列出){
$price=Product::getPriceStatic((int)$Product['id\u Product'],false,($Product['id\u Product\u attribute']?(int)$Product['id\u Product\u attribute']:null),6,null,false,true,$Product['cart\u quantity',false,(int)$order->id\u customer,(int)$order->id\u cart,(int)$order->{Configuration::get('PS\u TAX\u ADDRESS\u TYPE');
$price\u wt=Product::getPriceStatic((int)$Product['id\u Product'],true,($Product['id\u Product\u attribute']?(int)$Product['id\u Product\u attribute']:null),2,null,false,true,$Product['cart\u quantity',false,(int)$order->id\u customer,(int)$order->id\u cart,(int)$order->{Configuration::get('PS\u TAX\u ADDRESS\u TYPE');
$product\U price=product::getTaxCalculationMethod()==PS\U TAX\U EXC?工具::PS\U round($price,2):$price\U wt;
//这是一份日期报告
$idProduct=(int)$product['id_product'];
$SellerInfoOverride=Db::getInstance()->执行('SELECT*FROM`.\u Db\u PREFIX.'wk\u mp\u seller\u product`WHERE`id\u ps\u product`='(int)$idProduct);
//印刷费($SellerInfoOverride);
foreach($SellerInfoOverride作为$key=>$value){
$sellerid=$value['id_seller'];
//$seller=$mpSeller->getSeller($value['id\u seller'],$this->context->language->id);
$SellerInfoDetails=Db::getInstance()->执行('SELECT*FROM`.\u Db\u PREFIX.'wk\u mp\u seller`WHERE`id\u seller`='(int)$sellerid);
foreach($sellerinfoodetails作为$key=>$value){
$seller_name=$value['shop_name_unique'];
}
}
$product\U var\U tpl=数组(
“名称”=>$product['name'](isset($product['attributes'])?“-”.$product['attributes']:”),
“单价”=>Tools::displayPrice($product\u price,$this->context->currency,false),
“价格”=>Tools::displayPrice($product\U price*$product['quantity',$this->context->currency,false),
“数量”=>$product[“数量”],
“参考”=>$seller\u name,
'自定义'=>数组()
);
usort($product_var_tpl,function($a,$b){
返回$a['reference']-$b['reference'];
});
$customized_data=Product::getAllCustomizedData((int)$order->id_cart);
if(isset($customized_data[$product['id_product']][$product['id_product_attribute']])){
$product_var_tpl['customization']=array();
foreach($customized_data[$product['id_product'][$product['id_product_attribute']][$order->id_address_delivery]作为$customize){
$customization_text='';
if(isset($customization['datas'][Product::customization\u TEXTFIELD])){
foreach($customization['datas'][Product::customization_TEXTFIELD]作为$text){
$customization_text.=$text['name'].:'.$text['value'].
;
}
}
if(isset($customization['datas'][Product::customization_FILE])){
$customization_text.=sprintf(工具::显示错误(“%d个图像”),计数($customization['datas'][Product::customization_FILE])。
;
}
$customization_quantity=(int)$product['customization_quantity'];
$product\U var\U tpl['customization'][]=数组(
“自定义文本”=>$customization\u text,
“定制数量”=>$customization\u数量,
“数量”=>Tools::displayPrice($customization\u quantity*$product\u price,$this->context->currency,false)
);
}
}
$product\U var\U tpl\U list[]=$product\U var\U tpl;
//检查是否不是用于显示装运的虚拟产品
如果(!$product['is_virtual']){
$virtual_产品&=false;
}
}//结束foreach($products)
请对$product\u var\u tpl\u列表进行排序
,而不是$product\u var\u tpl
下面是示例代码
$array = array( // $product_var_tpl_list
array( // $product_var_tpl 1
'name' => 'b',
'reference' => 'bbb'
),
array( // $product_var_tpl 2
'name' => 'a',
'reference' => 'aaa'
),
array( // $product_var_tpl 3
'name' => 'd',
'reference' => 'ddd'
),
array( // $product_var_tpl 4
'name' => 'c',
'reference' => 'ccc'
),
array( // $product_var_tpl 5
'name' => 'e',
'reference' => 'eee'
),
array( // $product_var_tpl 6
'name' => 'a',
'reference' => 'www'
)
);
usort($array, function($a, $b) {
return strcmp($a['reference'], $b['reference']);
});
print_r($array);
试验结果如下
Array
(
[0] => Array
(
[name] => a
[reference] => aaa
)
[1] => Array
(
[name] => b
[reference] => bbb
)
[2] => Array
(
[name] => c
[reference] => ccc
)
[3] => Array
(
[name] => d
[reference] => ddd
)
[4] => Array
(
[name] => e
[reference] => eee
)
[5] => Array
(
[name] => a
[reference] => www
)
)
您的数组是一个具有一个
参考
键的关联数组。您希望如何拥有多个参考
键?您所提供的输入的预期输出是什么?您希望在此处排序什么?$product\u var\u tpl
难道不只是一项商品吗?输入和预期输出是什么?我已经编辑了我的问题您的$product\u var\u tpl很简单