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.
return two arrays from one
Moderator: General Moderators
Re: return two arrays from one
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;
}
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: return two arrays from one
Thanks for your help I have solved the problem now using ahowell's approach - very good work!