Page 1 of 1

Imploded array elements still being returned as "Array&

Posted: Mon Dec 12, 2005 9:30 pm
by IceMetalPunk
In the code below, I explode a string, then explode some of its array elements. I manipulate them, then I implode the array elements, and implode the original array, thus getting the entire string with some manipulated elements back, while still being able to parse the information at a later date.

However, many times the elements of the main array simply get returned as the word "Array", even though they are always imploded into a string. I can't figure out why.

Here's the code:

Code: Select all

<?php
 if ($ouid<1 || $ouid=="") {
exit("<b><u><center>Please log in to the Twisted Designs Graphics forum and make sure 'Remember Me' is checked YES before attempting to adjust a member's grade.<br><br><a href='JavaScript:window.close()'>Back to Profile (Close This Window)</a></b></u></center>");
}

$all="";
if (file_exists("grades.txt")) { $all=file_get_contents("grades.txt"); }
$all=explode("\r\n",$all);

$stop=0;
$newgrade=$grade;
for ($i=0; $i<count($all); $i++) {
if ($all[$i]=="") { unset($all[$i]); continue; }
$all[$i]=explode("|",$all[$i]);
if ($all[$i][0]==$uid) { $all[$i][1]=(double)$grade+(double)$all[$i][1]; $all[$i][2]=(double)1+(double)$all[$i][2]; $newgrade=$all[$i][1]/$all[$i][2]; $stop=1; break; }
$all[$i]=implode("|",$all[$i]);
}

if ($stop==0) {
$all[$i+1]=$uid."|".$grade."|1";
}

for ($i=0; $i<count($all); $i++) { if ($all[$i]=="") { unset($all[$i]); } }

$all=implode("\r\n",$all);
$f=fopen("grades.txt","w");
fwrite($f,$all);
fclose($f);

echo "<b><u><center>Thank You. Your Grade Has Been Entered And Averaged Into The Member's Total Grade. His/Her Grade Is Now ".$newgrade."%.<br><br><a href='JavaScript:window.close()'>Back to Profile (Close This Window)</a></b></u></center>";

?>
Can anyone help me, please?

Posted: Mon Dec 12, 2005 9:40 pm
by neophyte
You've got arrays inside of arrays some how. To view the structure of your array:

Code: Select all

<pre>
<?php print_r($array); ?>
</pre>
You can access the arrays within the arrays by:

Code: Select all

$array[$iteration][$key]
Intentionally or unintentionally you've created arrays within arrays.

Hope that helps

Posted: Tue Dec 13, 2005 11:53 am
by IceMetalPunk
Yes, I know that. That was intentional.

However, I implode the mini-arrays (the 2nd dimension of the array) in the same FOR loop that I explode them with no conditions required. So how can it still be an array after the FOR loop is over?

That's the one thing I can't understand. And if I can't understand WHY it happens, I can't fix it.

-IMP ;) :)

Posted: Tue Dec 13, 2005 12:20 pm
by choppsta
The reason is you're breaking out of the for loop before you've imploded if the uid is found.
I don't wanna be a coding nazi, but if you format your code better it becomes much clearer!!

Code: Select all

for ($i = 0; $i < count($all); $i++) 
{
	if ($all[$i] == "") 
	{ 
		unset($all[$i]); 
		continue; 
	}

	$all[$i] = explode("|", $all[$i]);

	if ($all[$i][0] == $uid) 
	{ 
		$all[$i][1] = (double) $grade + (double) $all[$i][1]; 
		$all[$i][2] = (double) 1 + (double) $all[$i][2]; 
		$newgrade = $all[$i][1] / $all[$i][2]; 
		$stop = 1; 
		break; // HERE, add before: $all[$i] = implode("|", $all[$i]);
	}

	$all[$i] = implode("|", $all[$i]);
}

Posted: Tue Dec 13, 2005 12:46 pm
by IceMetalPunk
OH! Thanks so much!

I can't believe I didn't notice that, but it explains why it only happened to the lines where the id was found, and that caused it to reset each ID line,.

I'll try it, and it will *hopefully* work.

Thanks again.

-IMP ;) :)

*EDIT* BTW, the reason my format is so bad is because I'm used to other languages, and also I'm new to PHP (a few months into it).