stack
stack

Reputation: 10228

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

I have an array which always contains 10 items. I need to sort them half per 5. I don't know English and I cannot say what I want exactly. Ok lets say an example:

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

I want to sort $arr like this:

$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;
    }
}

Upvotes: 0

Views: 44

Answers (5)

Fayaz
Fayaz

Reputation: 1191

Assuming you are simply trying to rearrange the index, the CODE is very simple:

$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

  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:

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

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

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

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

    ...

Upvotes: 2

Rahul
Rahul

Reputation: 18567

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.

Upvotes: 1

Pramod Patil
Pramod Patil

Reputation: 2763

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);
?>

Upvotes: 0

Louis Loudog Trottier
Louis Loudog Trottier

Reputation: 1387

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

if tthe array has different size, you can change te '5' by (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

Upvotes: -1

Afif Zafri
Afif Zafri

Reputation: 625

Just seperate your 10 items array into two separated array. Then just loop through the 5 item, and add them to new array.

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"
}
*/

Upvotes: -1

Related Questions