Php 按第一个子数组对多维数组排序
我有一个数组,我想对它进行排序:Php 按第一个子数组对多维数组排序,php,arrays,sorting,multidimensional-array,Php,Arrays,Sorting,Multidimensional Array,我有一个数组,我想对它进行排序: $ArrayStock = array(array("SIZE","L","XL","M"),("STOCK","12","3","2")); 如何按大小对数组进行排序 或者,我可以使用此数组作为起点: $ArrayStock = array(array("L","XL","M"),("12","3","2")); 你可以这样做: // Combine into one associative array $combined = array_combine(
$ArrayStock = array(array("SIZE","L","XL","M"),("STOCK","12","3","2"));
如何按大小对数组进行排序
或者,我可以使用此数组作为起点:
$ArrayStock = array(array("L","XL","M"),("12","3","2"));
你可以这样做:
// Combine into one associative array
$combined = array_combine($ArrayStock[0], $ArrayStock[1]);
// Rebuild it in the correct order:
foreach(["SIZE", "S","M","L","XL","XXL"] as $size) {
if (isset($combined[$size])) $result[$size] = $combined[$size];
}
// Split associative array back to its original structure:
$ArrayStock = [array_keys($result), array_values($result)];
请注意,这种结构在使用中并不实用。事实上,我会坚持使用关联数组。有几种方法可以剥这只猫的皮。我所有的方法都将产生预期的输出 首先,让我们在不重构阵列的情况下完成此操作: 接下来,我将展示一些可以操作重构数组的方法。特林科特的写作方式绝对没有问题。这些只是我想出的备选方案 我同意trincot关于使用大小作为键的观点,因为它们是唯一的,并且股票计数作为值。因此,第一个过程是生成新的数组结构:
$ArrayStock=[["L","XL","M"],["12","3","2"]];
1单直线阵列联合进近:
$new_structure=array_combine($ArrayStock[0],$ArrayStock[1]);
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
foreach($ArrayStock[0] as $i=>$size){
$new_structure[$size]=$ArrayStock[1][$i];
}
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
uksort($new_structure,function($a,$b)use($order){
return (array_search($a,$order)>array_search($b,$order)?1:-1);
// any unidentified sizes will go to the front of the array
});
// keep in mind, this doesn't declare $result, it sorts $new_structure
$result=array_replace( // replace values on matching keys
array_flip( // swap keys with values
array_intersect(
$order, // keep values from here
array_keys($new_structure) // that exist here
)
),
$new_structure); // use these elements for replace
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
// generate & iterate ordered array
foreach(array_intersect($order,array_keys($new_structure)) as $v){
$result[$v]=$new_structure[$v]; // build properly sorted array
}
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
2.每种方法:
$new_structure=array_combine($ArrayStock[0],$ArrayStock[1]);
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
foreach($ArrayStock[0] as $i=>$size){
$new_structure[$size]=$ArrayStock[1][$i];
}
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
uksort($new_structure,function($a,$b)use($order){
return (array_search($a,$order)>array_search($b,$order)?1:-1);
// any unidentified sizes will go to the front of the array
});
// keep in mind, this doesn't declare $result, it sorts $new_structure
$result=array_replace( // replace values on matching keys
array_flip( // swap keys with values
array_intersect(
$order, // keep values from here
array_keys($new_structure) // that exist here
)
),
$new_structure); // use these elements for replace
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
// generate & iterate ordered array
foreach(array_intersect($order,array_keys($new_structure)) as $v){
$result[$v]=$new_structure[$v]; // build properly sorted array
}
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
现在,要对新数组进行排序,您可以使用uksort或一系列其他数组函数/循环对预定顺序的数组进行排序:
$order=["XS","S","M","L","XL","2XL","3XL"]; // declare whatever appropriate sizes in order
1采用数组搜索方法的uksort:
$new_structure=array_combine($ArrayStock[0],$ArrayStock[1]);
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
foreach($ArrayStock[0] as $i=>$size){
$new_structure[$size]=$ArrayStock[1][$i];
}
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
uksort($new_structure,function($a,$b)use($order){
return (array_search($a,$order)>array_search($b,$order)?1:-1);
// any unidentified sizes will go to the front of the array
});
// keep in mind, this doesn't declare $result, it sorts $new_structure
$result=array_replace( // replace values on matching keys
array_flip( // swap keys with values
array_intersect(
$order, // keep values from here
array_keys($new_structure) // that exist here
)
),
$new_structure); // use these elements for replace
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
// generate & iterate ordered array
foreach(array_intersect($order,array_keys($new_structure)) as $v){
$result[$v]=$new_structure[$v]; // build properly sorted array
}
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
2个阵列替换为阵列翻转阵列相交阵列键方法:
$new_structure=array_combine($ArrayStock[0],$ArrayStock[1]);
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
foreach($ArrayStock[0] as $i=>$size){
$new_structure[$size]=$ArrayStock[1][$i];
}
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
uksort($new_structure,function($a,$b)use($order){
return (array_search($a,$order)>array_search($b,$order)?1:-1);
// any unidentified sizes will go to the front of the array
});
// keep in mind, this doesn't declare $result, it sorts $new_structure
$result=array_replace( // replace values on matching keys
array_flip( // swap keys with values
array_intersect(
$order, // keep values from here
array_keys($new_structure) // that exist here
)
),
$new_structure); // use these elements for replace
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
// generate & iterate ordered array
foreach(array_intersect($order,array_keys($new_structure)) as $v){
$result[$v]=$new_structure[$v]; // build properly sorted array
}
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
3.使用数组\交叉-数组\键的foreach方法:
$new_structure=array_combine($ArrayStock[0],$ArrayStock[1]);
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
foreach($ArrayStock[0] as $i=>$size){
$new_structure[$size]=$ArrayStock[1][$i];
}
// $new_structure = ['L'=>'12','XL'=>'3','M'=>'2']
uksort($new_structure,function($a,$b)use($order){
return (array_search($a,$order)>array_search($b,$order)?1:-1);
// any unidentified sizes will go to the front of the array
});
// keep in mind, this doesn't declare $result, it sorts $new_structure
$result=array_replace( // replace values on matching keys
array_flip( // swap keys with values
array_intersect(
$order, // keep values from here
array_keys($new_structure) // that exist here
)
),
$new_structure); // use these elements for replace
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
// generate & iterate ordered array
foreach(array_intersect($order,array_keys($new_structure)) as $v){
$result[$v]=$new_structure[$v]; // build properly sorted array
}
// $result=['M'=>'2','L'=>'12','XL'=>'3'];
最后,如trincot所示,您可以使用一个或多个行将排序后的数据恢复为初始格式:
$ArrayStock=[array_keys($result),array_values($result)];
你不能,因为出于某些愚蠢的原因,大小是数组的一部分,而不是一个键OK,如果我删除大小和库存,如何排序?比如$ArrayStock=arrayarrayL,XL,M,12,3,2;使其关联使usort函数将数组更改为$arrayStock=['size'=>[L=>['stock'=>12],XL=>['stock'=>3],M=>['stock'=>3]]可能更容易@亨基对冗长的回答感到抱歉。您可以最简单地使用我开始介绍的两步方法,也可以重新构造数组并使用许多技术中的任何一种对数据进行排序。我主要想用你的问题来教育读者。如果有什么对你不起作用或者你不理解我写的东西,请告诉我。我非常感谢Complete教程$ArrayStock@mickmackusa,我尝试了,它对我有用。@HengkyST这是我的荣幸。我总是通过实验/教学学到更多。