/    Sign up×
Community /Pin to ProfileBookmark

Merging every other array

just curious if someone knows how to merge every other sub array,

aka

[code=php]
$tmp = array();

$tmp[0] = array(false);
$tmp[1] = array(false);

$tmp[2] = array(false);
$tmp[3] = array(false);

$tmp[4] = array(false);
$tmp[5] = array(false);

[/code]

or ….
0+1
2+3
4+5

something like this i think?

[code=php]
$i=0; $new=array();
foreach($tmp as $k=>$v) {
$tmp[$k] = $v;
if($i=$k-1) { $new[] = $tmp[$i] + $tmp[$i-1]; /* or something ridiculous like that */}
$i++;
}
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@kromolNov 09.2011 — Maybe you want to do something like this:
[code=php]
$new = array();
foreach ($tmp as $k => $v)
{
if ((($k % 2) != 0) && ($k < count($tmp)))
{
$new[] = array_merge($tmp[$k - 1], $tmp[$k]);
}
}
[/code]
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...