...

View Full Version : For each value (Array within array)



MHaris
08-19-2007, 07:08 PM
<?php

$result = mysql_query("SELECT * FROM answers") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)){
$cID = $row['candidate_id'];
$binned_key = $cID;
$binned[$binned_key][] = $row["answer"];
}

$results = array();
foreach($binned as $key => $value){
$results[$key] = count(array_diff($user_answer, $value));
}

asort($results);
$closest = array_slice($results, 0, 3, 1);
foreach($closest as $key => $value){
$result = mysql_query("SELECT * FROM candidates WHERE id='$key'") or die(mysql_error());
while ($row = mysql_fetch_array($result)){
echo $row['name']."<br/>";
}
}
?>

The binned is Array => [1](candidate id) Array(answers)


foreach($binned as $key => $value){
$results[$key] = count(array_diff($user_answer, $value));
}

Here key is candidate id however value is Array and not the answers. How to get the value of the array within candidate id?

Fumigator
08-19-2007, 10:37 PM
Since $binned is a 2-dimensional array, you need to dip into the 2nd dimension of the array with another foreach loop.


foreach($binned as $key => $value) {
foreach($value as $answer) {
echo $answer; //will display the answer column from your query
}
}

MHaris
08-20-2007, 05:27 AM
Since $binned is a 2-dimensional array, you need to dip into the 2nd dimension of the array with another foreach loop.


foreach($binned as $key => $value) {
foreach($value as $answer) {
echo $answer; //will display the answer column from your query
}
}


Oh, thanks. :)

MHaris
08-20-2007, 05:40 AM
Oh, thanks. :)



foreach($binned as $key => $value){
foreach ($value => $child_value){
$results[$key] = count(array_diff($user_answer, $child_value));
}
}


Error: Parse error: syntax error, unexpected T_DOUBLE_ARROW in C:\wamp\www\quiz\templates\compare_answers.php on line 12

Inigoesdr
08-20-2007, 05:43 AM
foreach($binned as $key => $value){
foreach ($value => $child_value){
$results[$key] = count(array_diff($user_answer, $child_value));
}
}


Error: Parse error: syntax error, unexpected T_DOUBLE_ARROW in C:\wamp\www\quiz\templates\compare_answers.php on line 12

The second foreach should say foreach ($value as $child_value).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum