Jump to content

foreach Loops with varying keys


idire

Recommended Posts

I'm trying to get the results of a questionnaire out onto the page.

 

 

The two pieices of data I am concerned with are:

 

the question name

the number of people who chose that answer.

 

I originally had this array:

 

print_r($answers);

Array ( [0] => Array ( [0] => Array ( [A1] => 50 [NB] => 2 ) [1] => Array ( [A1] => 100 [NB] => 2 ) ) [1] => Array ( [0] => Array ( [A2] => 4 [NB] => 2 ) [1] => Array ( [A2] => 5 [NB] => 2 ) ) [2] => Array ( [0] => Array ( [A3] => 3 [NB] => 1 ) [1] => Array ( [A3] => 5 [NB] => 3 ) ) [3] => Array ( [0] => Array ( [A4] => 3 [NB] => 2 ) [1] => Array ( [A4] => 5 [NB] => 2 ) ) [5] => Array ( [0] => Array ( [b2] => 2 [NB] => 1 ) [1] => Array ( [b2] => 3 [NB] => 2 ) [2] => Array ( [b2] => 4 [NB] => 1 ) ) [6] => Array ( [0] => Array ( [b3] => 3 [NB] => 3 ) [1] => Array ( [b3] => 4 [NB] => 1 ) ) [8] => Array ( [0] => Array ( [C2] => 1 [NB] => 1 ) [1] => Array ( [C2] => 3 [NB] => 1 ) [2] => Array ( [C2] => 4 [NB] => 2 ) ) [10] => Array ( [0] => Array ( [C4] => No [NB] => 2 ) [1] => Array ( [C4] => Yes [NB] => 2 ) ) [11] => Array ( [0] => Array ( [D1] => 4 [NB] => 2 ) [1] => Array ( [D1] => 5 [NB] => 2 ) ) [12] => Array ( [0] => Array ( [D2] => 4 [NB] => 2 ) [1] => Array ( [D2] => 5 [NB] => 2 ) ) [13] => Array ( [0] => Array ( [D3] => 3 [NB] => 2 ) [1] => Array ( [D3] => 4 [NB] => 2 ) ) [14] => Array ( [0] => Array ( [D4] => 2 [NB] => 1 ) [1] => Array ( [D4] => 4 [NB] => 1 ) [2] => Array ( [D4] => 5 [NB] => 2 ) ) [15] => Array ( [0] => Array ( [E1] => 3 [NB] => 1 ) [1] => Array ( [E1] => 4 [NB] => 3 ) ) 

 

I used a foreach loop:

 

foreach($answers as $data)
{
//for each question
print_r($data);
}

which gives:

Array ( [0] => Array ( [A1] => 50 [NB] => 2 ) [1] => Array ( [A1] => 100 [NB] => 2 ) )
Array ( [0] => Array ( [A2] => 4 [NB] => 2 ) [1] => Array ( [A2] => 5 [NB] => 2 ) )
Array ( [0] => Array ( [A3] => 3 [NB] => 1 ) [1] => Array ( [A3] => 5 [NB] => 3 ) )
Array ( [0] => Array ( [A4] => 3 [NB] => 2 ) [1] => Array ( [A4] => 5 [NB] => 2 ) ) 
etc

 

then another foreach:

 

foreach($answers as $data)
{
//for each question
//print_r($data);
foreach($data as $value)
{
	print_r($value);
	//echo '<br/><br/>';
}
//new row
echo '<br/>';
}

which gives:

Array ( [A1] => 50 [NB] => 2 )
Array ( [A1] => 100 [NB] => 2 )


Array ( [A2] => 4 [NB] => 2 )
Array ( [A2] => 5 [NB] => 2 )


Array ( [A3] => 3 [NB] => 1 )
Array ( [A3] => 5 [NB] => 3 ) 

etc

 

What i need is a way to finally echo these values into tables. One for each anwer (A1, A2 etc)

 

But I cant just do echo $value[A1], $value[NB] as the index value for the first piece of data changes for each question, e.g. A1, A2 etc

 

Any ideas? This probably sounds very confused.

Link to comment
https://forums.phpfreaks.com/topic/136549-foreach-loops-with-varying-keys/
Share on other sites

Yes, i used that and got the same result as using foreach ($data as $value)

 

e.g.:

 

foreach($answers as $data)
{
//print_r($data);
foreach($data as $key => $value)
{
	print_r($value);
	//echo 'Answer: '.$value[A1].'<br/>';
	//echo 'Number of responses: '.$value[NB];
	echo '<br/><br/>';		
}
echo '<br/>';
}

gives:

Array ( [A1] => 50 [NB] => 2 )

Array ( [A1] => 100 [NB] => 2 )


Array ( [A2] => 4 [NB] => 2 )

Array ( [A2] => 5 [NB] => 2 )


Array ( [A3] => 3 [NB] => 1 )

Array ( [A3] => 5 [NB] => 3 ) 

 

what i need to echo is basically.

 

On question A1 2 people answered 100, and 2 people answered 50.

I did this:

 

<?php

foreach($answers as $data)
{
//for each question
//print_r($data);
foreach($data as $key => $value)
{
	$firstkey = key($value);
	//echo $firstkey;
	//print_r($value);
	echo 'Question: '.$firstkey.'<br/>';
	echo 'Answer: '.$value[$firstkey].'<br/>';
	echo 'Number of responses: '.$value[NB];
	echo '<br/><br/>';	
}
//new row
echo '<br/>';
}

?>

This gives me: 

Question: A1
Answer: 50
Number of responses: 2

Question: A1
Answer: 100
Number of responses: 2


Question: A2
Answer: 4
Number of responses: 2

Question: A2
Answer: 5
Number of responses: 2

etc

 

now just have to find out a way of grouping the answers into a new array which maps question name -> multiple ( answer + nb of responses )

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.