2

I have an array of variabel length, containing events sorted by startdate, looking like this:

Array
(
[0] => stdClass Object
    (
        [id] => 1978
        [date] => 2012-09-29
    )

[1] => stdClass Object
    (
        [id] => 1979
        [date] => 2012-10-14
    )

...etc....

I need to make a function that takes one of the event, and puts in the middle of a new array with a length of exactly seven, and puts the event's neighbours on each side.

So if event 5 passed to the function, the output should be:

[2][3][4][5][6][7][8]

If the first event is passed to the function, and the original amount of events is 12, the output should be:

[10][11][12][1][2][3][4]

If the orignal amount of events is 6, and the fifth event is passed, the output should be:

[2][3][4][5][6][1][]

So the list of events should always "wrap around" in the new array and fill it up as much as possible.

I have hacked together a solution, that involves quite a bit of steps. I dont really like it, and it got me wondering:

How would this be done in the most efficient and elegant way?

2
  • 1
    What's your current solution? What is your problem in using ksort() or usort()? Commented Jan 25, 2013 at 9:21
  • I have calculated how much the original array will be shifted to either the left or right, and what keys will be "cut off" from the shift. Then I create a new array and start filling it from [3] and then I add the cut off keys to either the left or the right side. It's quite handcoded and leaves me with the feeling, that I should be doing it some other way... Commented Jan 25, 2013 at 9:34

1 Answer 1

1
You need to change the values in this codes
  define('TO_SHOW',7); // number of items to show in your case it is 7
  $selected      = 1; //// which one you need need at center.

and your array;

Execute this and try :

<?php 

define('TO_SHOW',7); // number of items to show in your case it is 7

function change_order($arry, $sel){
   $arr_cnt      = count($arry);   
   $shift_count  = $arr_cnt - (ceil(TO_SHOW/2)-$sel);
   for($i=0; $i<$shift_count; $i++){
      array_push($arry, array_shift($arry));
   }
   return array_slice($arry, 0, TO_SHOW);
} 

$arr            = array(array(
                    "id" => 1,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 2,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 3,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 4,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 5,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 6,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 7,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 8,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 9,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 10,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 11,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 12,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 13,
                    "date" => 2012-09-29
                    ),
                    array(
                    "id" => 14,
                    "date" => 2012-09-29
                    )
                );

$selected      = 1; //// centre one             
$test   = change_order($arr, $selected);

echo "<pre>";
print_r($test);
?>
Sign up to request clarification or add additional context in comments.

7 Comments

Why is $selected an argument for the function, but TO_SHOW is a constant?
Your selected will be a variable rite? The one which you need in the center
Yes; but why is TO_SHOW not a variable as well? Seems like unnecessary global scope.
That will be a constant, how many items you want to show, in question it is 7 in all the cases.
Still, it's much better to have it as an argument to the function, not something that's global to the entire application.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.