0

次の配列があります

$array = array(
        'note' => array('test', 'test1', 'test2', 'test3', 'test4'),
        'year' => array('2011','2010', '2012', '2009', '2010'),
        'type' => array('journal', 'conference', 'conference', 'conference','conference'),
    );

uasort()そして、配列を使用して要素を並べ替えたいと思いますyear

やった:

function cmp($a, $b) {
   if($a['year'] == $b['year']) return 0;
   return ($a['year'] < $b['year']) ? -1 : 1;
} 

uasort($array,'cmp');

print_r($array);

しかし、出力は正しくありません:

Array
(
[type] => Array
    (
        [0] => journal
        [1] => conference
        [2] => conference
        [3] => conference
        [4] => conference
    )

[year] => Array
    (
        [0] => 2011
        [1] => 2010
        [2] => 2012
        [3] => 2009
        [4] => 2010
    )

[note] => Array
    (
        [0] => test
        [1] => test1
        [2] => test2
        [3] => test3
        [4] => test4
    )

)

望ましい出力:

Array
(
[type] => Array
    (
        [0] => conference
        [1] => journal
        [2] => conference
        [3] => conference
        [4] => conference
    )

[year] => Array
    (
        [0] => 2012
        [1] => 2011
        [2] => 2010
        [3] => 2010
        [4] => 2009
    )

[note] => Array
    (
        [0] => test2
        [1] => test
        [2] => test1
        [3] => test4
        [4] => test3
    )

)
4

2 に答える 2

1

入力が間違っているようです。次のようなことを試してください:

$array = array(
    array(
        'note' => 'test',
        'year' => 2011,
        'type' => 'journal',
    ),
    array(
        'note' => 'test1',
        'year' => 2010,
        'type' => 'conference',
    ),
    ...
);

あなたの例では、比較関数は「メモ」配列を「年」および「タイプ」配列と比較します。

于 2012-06-20T11:17:50.343 に答える
0

が必要ですがarray_multisort、必要ありませんuasort

入力がRonaldが言及した形式である場合、あなたのアプローチは機能します。

于 2012-06-20T11:18:38.243 に答える