简体   繁体   English

如何根据两列对数组的项目进行排序?

[英]How can I sort array's items based on two columns?

I have an array which always contains 10 items. 我有一个总是包含10个项目的数组。 I need to sort them half per 5. I don't know English and I cannot say what I want exactly. 我需要将它们每5分一半排序。我不懂英语,也无法确切地说出我想要的内容。 Ok lets say an example: 好的,举个例子:

$arr = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');

I want to sort $arr like this: 我想这样排序$arr

$expected_arr = array('one', 'six', 'two', 'seven', 'three', 'eight', 'four', 'nine', 'five', 'ten');

Is doing that possible? 有可能吗?


I guess I need something like this: 我想我需要这样的东西:

$expected_arr = array();
foreach( $arr as $key => $value ) {
    if ( $key  % 2 != 0 ) {
        $expected_arr[] = $value;
    }
}

Assuming you are simply trying to rearrange the index, the CODE is very simple: 假设您只是尝试重新排列索引,那么CODE非常简单:

$arr = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');


$expected_arr = array();
for ($i=0; $i < 5 ; $i++) { 
    $expected_arr[] = $arr[$i];
    $expected_arr[] = $arr[$i+5];
}

Once again, I've assumed: 再一次,我假设:

  1. Your array has only 10 elements 您的数组只有10个元素

  2. You simply want to insert index: 5,6,7,8,9 around 0,1,2,3,4, so that the new array will represent: 您只想在0、1、2、3、4周围插入索引:5、6、7、8、9,这样新数组将代表:

    $expected_arr[0] = $arr[0];

    $expected_arr[1] = $arr[5];

    $expected_arr[2] = $arr[1];

    $expected_arr[3] = $arr[6];

    ...

Try this simple and sweet, 试试这个简单而甜蜜的方法,

$arr = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');
$temp = [];
for($i = 0;$i < count($arr)-5;$i++){
   array_push($temp,$arr[$i],$arr[$i+5]);
}
print_r($temp);

Give it a try. 试试看。 This will work. 这将起作用。

Simple Solution 简单的解决方案

    <?php
$arr = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');
$split_arr = array_chunk($arr, 5);

$newArr = [];
foreach($split_arr[0] as $key => $val){
    $newArr[] = $val;
    $newArr[] = $split_arr[1][$key];
}
print_r($newArr);
?>

Just seperate your 10 items array into two separated array. 只需将10个项目数组分成两个单独的数组。 Then just loop through the 5 item, and add them to new array. 然后只需遍历5个项目,然后将它们添加到新数组中即可。

Example: 例:

// your array split to two new array.
$arr1 = array('one', 'two', 'three', 'four', 'five');
$arr2 = array('six', 'seven', 'eight', 'nine', 'ten');

$arrmerge = array(); // declare new empty array, used to join your new array

// itinerate through the 1st array, or 5 items (this only work if both new array have same size

for ($i=0; $i<count($arr1); $i++) 
{
   // store into new array, alternating, to achieve your needs
   $arrmerge[] = $arr1[$i];
   $arrmerge[] = $arr2[$i];
}

var_dump($arrmerge);
/*output
array(10) {
  [0]=>
  string(3) "one"
  [1]=>
  string(3) "six"
  [2]=>
  string(3) "two"
  [3]=>
  string(5) "seven"
  [4]=>
  string(5) "three"
  [5]=>
  string(5) "eight"
  [6]=>
  string(4) "four"
  [7]=>
  string(4) "nine"
  [8]=>
  string(4) "five"
  [9]=>
  string(3) "ten"
}
*/
array_sort($arr); //sorts the array,
$inc=(count($arr)/2);
for ($i = 1; $i <= $inc; $i++) { //run five times.
  $arrout[]=$arr[$i]; //add 1st value, (increment on loop)
  $arrout[]=$arr[($i+$inc)]; //add 6th value, (increment on loop)
}

unset($arr);
print_r($arrout);

Untested but should work. 未经测试,但应该可以。 See http://php.net/manual/en/array.sorting.php 参见http://php.net/manual/en/array.sorting.php

if tthe array has different size, you can change te ' 5 ' by (count($arr)/2) 如果里边反阵列具有不同的尺寸,可以改变TE“ 5 ”由(count($arr)/2)

Cheers 干杯

EDIT: should work with any lenght of array as long as it contain an even amount of value. 编辑:只要包含偶数个值,就可以使用任何长度的数组。 I know you sai dit alwways have 10 items but you know, expandable :P 我知道您sai dit总是有10个项目,但您知道,可扩展:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM