Page 1 of 1

return two arrays from one

Posted: Tue Sep 02, 2008 11:59 am
by jnorton
Hey All,

I have a problem that I can't figure out and desperately need some help:

I have an array containing a month date and a post count:

Array ( [1] => 1 [4] => 3 [9] => 2 [10] => 2 [11] => 4)

What I want to do is split the array at the current month integer (9) and return 2 arrays to the left and right of the current month key i.e.

left = Array ( [1] => 1 [4] => 3)
right = Array ( [10] => 2 [11] => 4)

Any help would be greatly appreciated.

Thanks in advance,

Justin.

Re: return two arrays from one

Posted: Tue Sep 02, 2008 12:09 pm
by ahowell

Code: Select all

$posts = array(
    1  => 1,
    4  => 3,
    9  => 2,
    10 => 2,
    11 => 4
);
 
 
$left  = array();
$right = array();
 
foreach ($posts as $month => $postCnt) {
    if ($month < date('n')) {
        $left[$month] = $postCnt;
    } else if ($month > date('n')) {
        $right[$month] = $postCnt;
    }
}

Re: return two arrays from one

Posted: Tue Sep 02, 2008 12:13 pm
by Christopher

Re: return two arrays from one

Posted: Wed Sep 03, 2008 10:23 am
by jnorton
Thanks for your help I have solved the problem now using ahowell's approach - very good work!