在 PHP 中将数组元素移动到顶部

发布于 2024-10-22 10:30:53 字数 152 浏览 7 评论 0原文

$arr = array(
    'a1'=>'1',
    'a2'=>'2'
);

我需要将 a2 移到顶部,并保留 a2 作为键,我该如何继续下去,我似乎无法想出一种方法而不搞砸一些东西:)

$arr = array(
    'a1'=>'1',
    'a2'=>'2'
);

I need to move the a2 to the top, as well as keep the a2 as a key how would I go on about it I can't seem to think a way without messing something up :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

伪装你 2024-10-29 10:30:53

这是一个可以正确使用数字键和字符串键的解决方案:

function move_to_top(&$array, $key) {
    $temp = array($key => $array[$key]);
    unset($array[$key]);
    $array = $temp + $array;
}

它之所以有效,是因为 PHP 中的数组是有序映射。
顺便说一句,要将项目移动到底部,请使用:

function move_to_bottom(&$array, $key) {
    $value = $array[$key];
    unset($array[$key]);
    $array[$key] = $value;
}

Here is a solution which works correctly both with numeric and string keys:

function move_to_top(&$array, $key) {
    $temp = array($key => $array[$key]);
    unset($array[$key]);
    $array = $temp + $array;
}

It works because arrays in PHP are ordered maps.
Btw, to move an item to bottom use:

function move_to_bottom(&$array, $key) {
    $value = $array[$key];
    unset($array[$key]);
    $array[$key] = $value;
}
榆西 2024-10-29 10:30:53

您可以通过以下方式实现:

$arr = array(
    'a1'=>'1',
    'a2'=>'2'
);

end($arr);

$last_key     = key($arr);
$last_value   = array_pop($arr);
$arr          = array_merge(array($last_key => $last_value), $arr);

/*
    print_r($arr);

    will output (this is tested):
    Array ( [a2] => 2 [a1] => 1 )
*/

You can achieve that this way:

$arr = array(
    'a1'=>'1',
    'a2'=>'2'
);

end($arr);

$last_key     = key($arr);
$last_value   = array_pop($arr);
$arr          = array_merge(array($last_key => $last_value), $arr);

/*
    print_r($arr);

    will output (this is tested):
    Array ( [a2] => 2 [a1] => 1 )
*/
相思故 2024-10-29 10:30:53

试试这个:

$key = 'a3';
$arr = [
    'a1' => '1',
    'a2' => '2',
    'a3' => '3',
    'a4' => '4',
    'a5' => '5',
    'a6' => '6'
];
if (isset($arr[ $key ]))
    $arr = [ $key => $arr[ $key ] ] + $arr;

结果:

array(
    'a3' => '3',
    'a1' => '1',
    'a2' => '2',
    'a4' => '4',
    'a5' => '5',
    'a6' => '6'
)

try this:

$key = 'a3';
$arr = [
    'a1' => '1',
    'a2' => '2',
    'a3' => '3',
    'a4' => '4',
    'a5' => '5',
    'a6' => '6'
];
if (isset($arr[ $key ]))
    $arr = [ $key => $arr[ $key ] ] + $arr;

result:

array(
    'a3' => '3',
    'a1' => '1',
    'a2' => '2',
    'a4' => '4',
    'a5' => '5',
    'a6' => '6'
)
无所谓啦 2024-10-29 10:30:53

这是一个简单的单行代码,可以使用 array_splice() 和 union 运算符来完成此操作:

$arr = array('a1'=>'1', 'a2'=>'2', 'a3' => '3');
$arr = array_splice($arr,array_search('a2',array_keys($arr)),1) + $arr;

编辑:

回想起来,我不确定为什么我要这样做不仅仅是这样做:

$arr = array('a2' => $arr['a2']) + $arr;

更干净、更容易而且可能更快。

Here's a simple one liner to get this done with array_splice() and the union operator:

$arr = array('a1'=>'1', 'a2'=>'2', 'a3' => '3');
$arr = array_splice($arr,array_search('a2',array_keys($arr)),1) + $arr;

Edit:

In retrospect I'm not sure why I wouldn't just do this:

$arr = array('a2' => $arr['a2']) + $arr;

Cleaner, easier and probably faster.

巷雨优美回忆 2024-10-29 10:30:53

您还可以查看 array_multisort 这可以让您使用一个数组对另一个进行排序。例如,这可以允许您将硬编码的值顺序外部化到配置文件中。

<?php
   $ar1 = array(10, 100, 100, 0);
   $ar2 = array(1, 3, 2, 4);
   array_multisort($ar1, $ar2);

   var_dump($ar1);
   var_dump($ar2);
?>

在此示例中,排序后,第一个数组将包含 0、10、100、100。第二个数组将包含 4、1、2、3。第二个数组中的条目与第一个数组中的相同条目相对应数组(100 和 100)也已排序。

输出:

array(4) {
  [0]=> int(0)
  [1]=> int(10)
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=> int(4)
  [1]=> int(1)
  [2]=> int(2)
  [3]=> int(3)
}

You can also look at array_multisort This lets you use one array to sort another. This could, for example, allow you to externalize a hard-coded ordering of values into a config file.

<?php
   $ar1 = array(10, 100, 100, 0);
   $ar2 = array(1, 3, 2, 4);
   array_multisort($ar1, $ar2);

   var_dump($ar1);
   var_dump($ar2);
?>

In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.

Outputs:

array(4) {
  [0]=> int(0)
  [1]=> int(10)
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=> int(4)
  [1]=> int(1)
  [2]=> int(2)
  [3]=> int(3)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文