PHP Sort array of arrays by object key [duplicate]

时间:2021-07-27 01:30:46

This question already has an answer here:

这个问题在这里已有答案:

I have a form where I'm creating a number of item arrays:

我有一个表单,我正在创建一些项目数组:

<input type="hidden" value="Full/Double Mattress" name="pickup1-dropoff1Items[1][0]">
<input type="text" name="pickup1-dropoff1Items[1][1]">
<input type="hidden" value="20" name="pickup1-dropoff1Items[1][2]">
<input type="hidden" value="FMat" name="pickup1-dropoff1Items[1][3]">
<input type="hidden" value="1" name="pickup1-dropoff1Items[1][4]">

so the structure is basically:

所以结构基本上是:

array(
    array('title', quantity, price, 'shorthand', order),
    array('title', quantity, price, 'shorthand', order)
)

etc...

I'm getting this information using PHP and sending it in an email. I can get one of these arrays like so:

我使用PHP获取此信息并通过电子邮件发送。我可以这样得到其中一个数组:

$pickup1_dropoff1Items = $_POST['pickup1-dropoff1Items'];

I would like to sort the arrays in $pickup1_dropoff1Items by the 'order' number (i.e. index #4, i.e. $pickup1-dropoff1Items[i][4]) in each of those arrays.

我想在$ pickup1_dropoff1Items中对每个数组中的'order'号(即索引#4,即$ pickup1-dropoff1Items [i] [4])对数组进行排序。

Can this be done using PHP ksort()? Does anyone have any idea how to sort an array like this using PHP?

可以使用PHP ksort()完成吗?有没有人知道如何使用PHP对这样的数组进行排序?

Thanks!

2 个解决方案

#1


1  

It's not tested but I think this will do what you need:

它没有经过测试,但我认为这将满足您的需求:

// first create a new array of just the order numbers 
// in the same order as the original array
$orders_index = array();
foreach( $pickup1_dropoff1Items as $item ) {
  $orders_index[] = $item[4];
}

// then use a sort of the orders array to sort the original
// array at the same time (without needing to look at the 
// contents of the original)
array_multisort( $orders_index, $pickup1_dropoff1Items );

This is essentially example 1 here: http://www.php.net/manual/en/function.array-multisort.php but our $ar2 is an array of arrays instead of an array of single values. Also if you need more control over the sort you'll see examples of options you can use at that URL: just add them to the list of arguments for array_multisort.

这基本上是示例1:http://www.php.net/manual/en/function.array-multisort.php但我们的$ ar2是一个数组数组而不是单个数组的数组。此外,如果您需要更多控制排序,您将看到可以在该URL使用的选项示例:只需将它们添加到array_multisort的参数列表中。

#2


1  

For sorting complex arrays like this, you can use something like usort() which "sorts an array by values using a user-defined comparison function".

对于像这样的复杂数组的排序,你可以使用类似usort()的东西,它“使用用户定义的比较函数按值对数组进行排序”。

See the example on php.net for more information.

有关更多信息,请参阅php.net上的示例。

#1


1  

It's not tested but I think this will do what you need:

它没有经过测试,但我认为这将满足您的需求:

// first create a new array of just the order numbers 
// in the same order as the original array
$orders_index = array();
foreach( $pickup1_dropoff1Items as $item ) {
  $orders_index[] = $item[4];
}

// then use a sort of the orders array to sort the original
// array at the same time (without needing to look at the 
// contents of the original)
array_multisort( $orders_index, $pickup1_dropoff1Items );

This is essentially example 1 here: http://www.php.net/manual/en/function.array-multisort.php but our $ar2 is an array of arrays instead of an array of single values. Also if you need more control over the sort you'll see examples of options you can use at that URL: just add them to the list of arguments for array_multisort.

这基本上是示例1:http://www.php.net/manual/en/function.array-multisort.php但我们的$ ar2是一个数组数组而不是单个数组的数组。此外,如果您需要更多控制排序,您将看到可以在该URL使用的选项示例:只需将它们添加到array_multisort的参数列表中。

#2


1  

For sorting complex arrays like this, you can use something like usort() which "sorts an array by values using a user-defined comparison function".

对于像这样的复杂数组的排序,你可以使用类似usort()的东西,它“使用用户定义的比较函数按值对数组进行排序”。

See the example on php.net for more information.

有关更多信息,请参阅php.net上的示例。