php php关联数组键顺序(非排序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4896623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php associative array key order (not sort)
提问by nizzle
My array:
我的阵列:
$data = array('two' => 2, 'one' => 1, 'three' => 3);
Now, with when I iterate the array, the first value that will come up will probably be
现在,当我迭代数组时,出现的第一个值可能是
$data['two'] // = 2 @ index[0]
right?
对?
What if I want to move the $data[1] to the position of $data[0] ?
如果我想将 $data[1] 移动到 $data[0] 的位置怎么办?
To rephrase:
改写:
How do I make the array look like this (so that 'one' comes up at $data[0])
我如何使数组看起来像这样(以便“一个”出现在 $data[0] 处)
$data = array('one' => 1, 'two' => 2, 'three' => 3
Why do I need this?
为什么我需要这个?
I use code igniter, the table->generate built-in function takes an assoc array and creates a table but offers no method of arranging the columns. This is why I would like to move the columns in the source array.
我使用代码点火器,table->generate 内置函数接受一个 assoc 数组并创建一个表,但没有提供排列列的方法。这就是为什么我想移动源数组中的列。
采纳答案by Mark Baker
Take a look at daniele centamore's commenton PHP's array_splice()function, where he provides a couple of functions for moving the elements in an non-associative array.
看看daniele centamore对 PHP 的array_splice()函数的评论,他提供了几个函数来移动非关联数组中的元素。
<?php
// $input (Array) - the array containing the element
// $index (int) - the index of the element you need to move
function moveUp($input,$index) {
$new_array = $input;
if((count($new_array)>$index) && ($index>0)){
array_splice($new_array, $index-1, 0, $input[$index]);
array_splice($new_array, $index+1, 1);
}
return $new_array;
}
function moveDown($input,$index) {
$new_array = $input;
if(count($new_array)>$index) {
array_splice($new_array, $index+2, 0, $input[$index]);
array_splice($new_array, $index, 1);
}
return $new_array;
}
$input = array("red", "green", "blue", "yellow");
$newinput = moveUp($input, 2);
// $newinput is array("red", "blue", "green", "yellow")
$input = moveDown($newinput, 1);
// $input is array("red", "green", "blue", "yellow")
?>
回答by Diego Pino
Two possible solutions (without using array_splice
):
两种可能的解决方案(不使用array_splice
):
1) Create a new array with the new order of the keys.
1) 使用新的键顺序创建一个新数组。
$new_keys = array('one', 'two', 'three');
$new_data = array();
foreach ($new_keys as $key) {
$new_data[$key] = $data[$key];
}
$data = $new_data;
2) Move the element one
upfront, remove it from $data
and copy the rest of the array.
2) 将元素one
向前移动,将其从$data
数组中移除并复制数组的其余部分。
function rearrangeData($data) {
$result['one'] = $data['one'];
unset($data['one']);
return array_merge($result, $data);
}
$data = rearrangeData($data);
回答by oriadam
Here's a working example:
这是一个工作示例:
<?php
$data = array('two' => 2, 'one' => 1, 'three' => 3);
print_r($data);
ksort($data);
echo "ksort:\n";
print_r($data);
uksort($data,'cmp');
echo "uksort:\n";
print_r($data);
function cmp($a, $b)
{
$num=' one two three four five six seven eight nine ten';
$ai = stripos($num,$a);
$bi = stripos($num,$b);
if ($ai>0 && $bi>0) {
return ($ai > $bi) ? 1 : -1;
}
return strcasecmp($a, $b);
}
Output:
输出:
Array
(
[two] => 2
[one] => 1
[three] => 3
)
ksort:
Array
(
[one] => 1
[three] => 3
[two] => 2
)
uksort:
Array
(
[one] => 1
[two] => 2
[three] => 3
)
Run this: http://codepad.org/yAK1b1IP
运行这个:http: //codepad.org/yAK1b1IP
回答by Dan Grossman
PHP has 13 functions for sorting arrays, by key, by value, by user-defined functions where you can specify that "one" comes before "two". There's also array_shift, array_unshift, array_push and array_pop for moving things onto or off the front or end of the array. You can build a whole new array from the existing one.
PHP 有13 个用于按键、按值、按用户定义的函数对数组进行排序的函数,您可以在其中指定“一”在“二”之前。还有 array_shift、array_unshift、array_push 和 array_pop 用于将事物移入或移出数组的前端或后端。您可以从现有阵列构建一个全新的阵列。
回答by Distdev
I think, you should use asort function:
我认为,你应该使用 asort 函数:
$data = array('two' => 2, 'one' => 1, 'three' => 3);
$dataOrdered = $data;
asort($dataOrdered);