/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Array Missing Last Value

I have 2 arrays that I’m putting into one called, $umbrellaArray:

[code=php]$o++; $d++;
$orderArray[$o] = $q2_order;
$descriptionArray[$d] = $q2_description;

$umbrellaArray = array();
for ($i = 0; $i < count($orderArray); ++$i) {
if ($i > 0) {$umbrellaArray[$orderArray[$i]] = $descriptionArray[$i];}
}[/code]

The problem is that count($orderArray) = 9 but doing a foreach construct on $umbrellaArray prints only 8 values.

Thanks in advance.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@StevishMar 16.2009 — When you construct this array, what key do you start with, 0 or 1? Your current for loop would start working with $i at 1, then continue to 8 (assuming count($orderarray) == 9).

Try changing your for loop to for($i=0; $i <= count($orderArray); ++$i) {...

ps. Why are you using a for loop there instead of foreach? Here's how I would code it:

[code=php]
foreach ($orderArray as $key => $val) {
$umbrellaArray[$val] = $descriptionArray[$key];
}
[/code]
Copy linkTweet thisAlerts:
@rbraggauthorMar 16.2009 — I just changed from:

$i < count($orderArray)

to:

$i <= count($orderArray)

This worked PERFECTLY! Thank you! ?
Copy linkTweet thisAlerts:
@StevishMar 16.2009 — No problem. Look into using the foreach though... it's usually cleaner that way.
×

Success!

Help @rbragg 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...