Jump to content

[SOLVED] undefined offset error in a WHILE loop


mzhou88

Recommended Posts

$output = array();

while (list($key,$value) = each($lines)) {
if (($key >= $startNo) && ($key < ($startNo+$xf_config_postsystem_posts_per_page))) {
	$output[$key] .= $value;
}
}

return $output;

 

that code gives me an undefined offset error, and i don't know how to fix it... i've tried $output = array(); and i've tried $output = ''; and other similar line before calling the loop. any help appreciated

 

- thanks

$output[$key] .= $value;

 

This is appending the string $value to $output[$key], like this:

 

$output[1] .= 'world';

 

So if $output[1] does not exist, you get a key error.

 

So check that it exists first:

 

if( array_key_exists($key, $output))
{
  // it exists, so we can append to it:
  $output[$key] .= $value;
}
else
{
  // does not exist, so we create it:
  $output[$key] = $value;
}

 

 

Take careful note of = and .= in the code.

 

monk.e.boy

ahh.. thankyou thankyou.. it works. but it will output all the $output[$key] in this case, how do i limit $key to display only some of it, for example, $key is between 4 and 10 ?

 

thanks =p

 

 

eeerm,

 

if( ($key>4) && ($key<10 ))

 

Not rocket science.

 

monk.e.boy

sorry i guess i shoulda been specific, what i did earlier was add my piece of code with yours and i end up with warning messages that said for the function array_key_exists() the 2nd parameter shoulda been an array or object. so i was just wondering how you put together the code, cause im noobie

 

thanks =p

 

edit//

nvm i was being an idiot, mis typed some punction.. thanks =p

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.