Jump to content

foreach loop odd results


aebstract

Recommended Posts

foreach ( $indexL[$var] as $val ){
    foreach ( $val as $key=>$final_val ){
	$content .= "$key[firstname]";
}
    $content .= "<br>";
}

 

Here are the results from a print_r($indexL[$var]);

 

Array

(

    [0] => Array

        (

            [firstname] => Mike

            [lastname] => Herring

            [q1] => 4.616

            [q2] => 4.669

            [q3] => 4.686

            [regid] => 118

            [eventid] => 1

        )

 

)

 

Array

(

    [0] => Array

        (

            [firstname] => Adam

            [lastname] => Garner

            [q1] => 5.286

            [q2] => 5.286

            [q3] => 5.286

            [regid] => 109

            [eventid] => 1

        )

 

    [1] => Array

        (

            [firstname] => Ronnie

            [lastname] => Clayton

            [q1] => 5.219

            [q2] => 5.228

            [q3] => 5.228

            [regid] => 155

            [eventid] => 1

        )

 

)

 

Array

(

)

 

Array

(

    [0] => Array

        (

            [firstname] => Terry

            [lastname] => McClendon

            [q1] => 6.998

            [q2] => 6.998

            [q3] => 6.998

            [regid] => 83

            [eventid] => 1

        )

 

)

 

My output looks like:

 

flqqqre

 

flqqqre

flqqqre

 

flqqqre

 

 

I tried to use $final_val[firstname] but it gave me odd results too.

Link to comment
https://forums.phpfreaks.com/topic/200036-foreach-loop-odd-results/
Share on other sites

What are the bullet points?

 

Also, why did you use a foreach loop if you're just going to display firstname all the time?

 

I guess they got changed to bullets, it's indexes [0] [1] etc, none of them go over 1.

Here is the full array for $indexL:

 

Array

(

    [4.70 Index] => Array

        (

            [ 0 ] => Array

                (

                    [firstname] => Mike

                    [lastname] => Herring

                    [q1] => 4.616

                    [q2] => 4.669

                    [q3] => 4.686

                    [regid] => 118

                    [eventid] => 1

                )

 

        )

 

    [5.30 Index] => Array

        (

            [ 0 ] => Array

                (

                    [firstname] => Adam

                    [lastname] => Garner

                    [q1] => 5.286

                    [q2] => 5.286

                    [q3] => 5.286

                    [regid] => 109

                    [eventid] => 1

                )

 

            [ 1 ] => Array

                (

                    [firstname] => Ronnie

                    [lastname] => Clayton

                    [q1] => 5.219

                    [q2] => 5.228

                    [q3] => 5.228

                    [regid] => 155

                    [eventid] => 1

                )

 

        )

 

    [6.00 Index] => Array

        (

        )

 

    [7.00 Index] => Array

        (

            [ 0 ] => Array

                (

                    [firstname] => Terry

                    [lastname] => McClendon

                    [q1] => 6.998

                    [q2] => 6.998

                    [q3] => 6.998

                    [regid] => 83

                    [eventid] => 1

                )

 

        )

 

)

 

I put spaces so it wouldn't change. I plan on doing other things with the values in the array, I was just trying to get a grasp on how to control the values and so far I can't get the value.

 

 

 

 

teamatomic:

When I try that code, I get this output:

 

4.70

M

H

4

4

4

1

1

 

5.30 Index

A

G

5

5

5

1

1

R

C

5

5

5

1

1

 

6.00 Index

 

 

7.00 Index

T

M

6

6

6

8

1

$query2 = mysql_query("
SELECT registrations.firstname, participants.eventid, registrations.lastname, participants.q1, participants.q2, participants.q3, participants.q1s, participants.q2s, participants.q3s, participants.q1rt, participants.q2rt, participants.q3rt, participants.regid
FROM participants
INNER JOIN registrations ON registrations.id = participants.regid
WHERE participants.eventid = $_GET[event] AND registrations.class = '$var'") or DIE(mysql_error());
if(mysql_num_rows($query)!=0){

$indexL[$var] = array();
$indexH[$var] = array();

	while($r2=mysql_fetch_array($query2))
	{

	$var2 = substr($var,0,4);
	if ((($r2[q1] + $r2[q1rt]) < $var2) && (($r2[q2] + $r2[q2rt]) < $var2) && (($r2[q3] + $r2[q3rt]) < $var2)){

$indexL[$var][] = array('firstname' => "$r2[firstname]", 'lastname' => "$r2[lastname]", 'q1' => "$r2[q1]", 'q2' => "$r2[q2]", 'q3' => "$r2[q3]", 'regid' => "$r2[regid]", 'eventid' => "$r2[eventid]");

} else {

$indexH[$var][] = array('firstname' => "$r2[firstname]", 'lastname' => "$r2[lastname]", 'q1' => "$r2[q1]", 'q2' => "$r2[q2]", 'q3' => "$r2[q3]", 'regid' => "$r2[regid]", 'eventid' => "$r2[eventid]");

}


}
}


$countH = count($indexH[$var]);
$countL = count($indexL[$var]);


foreach ( $indexL[$var] as $val ){
    foreach ( $val as $final_val ){
      $content .= sprintf("%s <br/>", $final_val['firstname']);
   }
}

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.