-1

我有两个数组。第一个是我的对象:

Array
(
[0] => stdClass Object
    (
        [id_keyword] => 148
        [id_group] => 34
    )

[1] => stdClass Object
    (
        [id_keyword] => 200
        [id_group] => 34
    )

[2] => stdClass Object
    (
        [id_keyword] => 151
        [id_group] => 34
    )

[3] => stdClass Object
    (
        [id_keyword] => 207
        [id_group] => 34
    )
)

还有一个,我需要基于 id_keyword 的位置:

Array
(
   [0] => 200
   [1] => 148
   [2] => 151
   [3] => 207
)

我想对我的第一个数组进行排序。我查看了array_multisort,但没有成功:/

任何想法 ?

谢谢 !

4

1 回答 1

1

在这种情况下,如果可以的话,我会使用该id_keyword值作为第一个数组的键。因此,在创建对象时,它现在看起来像

<?php
  $my_object = new stdClass();
  $my_object->id_keyword = $a_value;
  $my_object->.... other variables here

  // and when the object is added in the array
  $my_array[] = $my_object;
?>

而不是最后一行,我建议

<?php
  $my_array[$my_object->id_keyword] = $my_object;
?>

然后使用ksort对数组进行排序。

我能想到的唯一其他方法是使用usort.

于 2013-09-23T10:21:55.817 回答