用子值对多维数组进行排序,并维护主索引。

时间:2022-04-27 02:23:52

I have a multidimensional array like this.

我有一个这样的多维数组。

arr[4]=array('name' => 'ab', 'aid' => 1 'qty' => 10);
arr[5]=array('name' => 'ac', 'aid' => 3 'qty' => 15);
arr[3]=array('name' => 'ad', 'aid' => 2 'qty' => 100);
arr[2]=array('name' => 'ae', 'aid' => 2 'qty' => 150);

and I need to sort them by the 'aid' but don't change the main index like this.

我需要用“援助”来分类,但不要像这样改变主索引。

arr[4]=array('name' => 'ab', 'aid' => 1 'qty' => 10);
arr[2]=array('name' => 'ae', 'aid' => 2 'qty' => 150);
arr[3]=array('name' => 'ad', 'aid' => 2 'qty' => 100);
arr[5]=array('name' => 'ac', 'aid' => 3 'qty' => 15);

is that possible? what should I do? thanks.

这有可能吗?我应该做什么?谢谢。

1 个解决方案

#1


0  

From PHP manual

从PHP手册

<?php
    $data[] = array('volume' => 67, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 1);
    $data[] = array('volume' => 85, 'edition' => 6);
    $data[] = array('volume' => 98, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 6);
    $data[] = array('volume' => 67, 'edition' => 7);
?>

In this example, we will order by volume descending, edition ascending.

在本例中,我们将按音量下降、版本提升来排序。

We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

我们有一个行数组,但是array_multisort()需要一个列的数组,所以我们使用下面的代码来获取列,然后执行排序。

<?php
    // Obtain a list of columns
    foreach ($data as $key => $row) {
        $volume[$key]  = $row['volume'];
        $edition[$key] = $row['edition'];
    }

    // Sort the data with volume descending, edition ascending
    // Add $data as the last parameter, to sort by the common key
    array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

http://php.net/manual/en/function.array-multisort.php

http://php.net/manual/en/function.array-multisort.php

Edit

编辑

For your need you requested:

为了您的需要,您需要:

$arr[4]=array('name' => 'ab', 'aid' => 1, 'qty' => 10);
$arr[5]=array('name' => 'ac', 'aid' => 3, 'qty' => 15);
$arr[3]=array('name' => 'ad', 'aid' => 2, 'qty' => 100);
$arr[2]=array('name' => 'ae', 'aid' => 2, 'qty' => 150);

$keyPositions = array();
foreach ($arr as $key => $value) {
    $keyPositions[] = $key;
}


foreach ($arr as $key => $row) {
    $aid[$key]  = $row['aid'];
}

array_multisort($aid, SORT_ASC, $arr);

$sortedArray = array();
foreach ($arr as $key => $value) {
    $sortedArray[$keyPositions[$key]] = $value;
}

echo '<pre>';
print_r($sortedArray);
echo '</pre>';

?>

#1


0  

From PHP manual

从PHP手册

<?php
    $data[] = array('volume' => 67, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 1);
    $data[] = array('volume' => 85, 'edition' => 6);
    $data[] = array('volume' => 98, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 6);
    $data[] = array('volume' => 67, 'edition' => 7);
?>

In this example, we will order by volume descending, edition ascending.

在本例中,我们将按音量下降、版本提升来排序。

We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

我们有一个行数组,但是array_multisort()需要一个列的数组,所以我们使用下面的代码来获取列,然后执行排序。

<?php
    // Obtain a list of columns
    foreach ($data as $key => $row) {
        $volume[$key]  = $row['volume'];
        $edition[$key] = $row['edition'];
    }

    // Sort the data with volume descending, edition ascending
    // Add $data as the last parameter, to sort by the common key
    array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

http://php.net/manual/en/function.array-multisort.php

http://php.net/manual/en/function.array-multisort.php

Edit

编辑

For your need you requested:

为了您的需要,您需要:

$arr[4]=array('name' => 'ab', 'aid' => 1, 'qty' => 10);
$arr[5]=array('name' => 'ac', 'aid' => 3, 'qty' => 15);
$arr[3]=array('name' => 'ad', 'aid' => 2, 'qty' => 100);
$arr[2]=array('name' => 'ae', 'aid' => 2, 'qty' => 150);

$keyPositions = array();
foreach ($arr as $key => $value) {
    $keyPositions[] = $key;
}


foreach ($arr as $key => $row) {
    $aid[$key]  = $row['aid'];
}

array_multisort($aid, SORT_ASC, $arr);

$sortedArray = array();
foreach ($arr as $key => $value) {
    $sortedArray[$keyPositions[$key]] = $value;
}

echo '<pre>';
print_r($sortedArray);
echo '</pre>';

?>