Jump to content

[SOLVED] Need help on making a true/false from explode/for's


viion

Recommended Posts

I am trying to make a code Explode a string, then see if the values of the string match another string, and which values do = true, which values don't = false, what is the best way to do this?

 

I so far got this: If kind of works but for some reason when I do if (empty($outcome)) it's counting 0 as Empty, when it isn't.

<?
// Database Section. Pull All Information to Parse.
//-------------------------------------------------------------------------------------------------------
// Pull user's choice from database.
$users_choice = ("Kirin Osode|Hagun|Unji");
echo ("<strong>User's Choice:</strong> $users_choice");
echo ("<hr />");
// Pull all the possible Choices.
$possible_choice = ("Kirin Osode|Unji|Unsho|Hagun|Plastic Belt");
echo ("<strong>Possible Choices:</strong> $possible_choice");
echo ("<hr />");
//-------------------------------------------------------------------------------------------------------
// Explode user's choice into an array.
// Always remember that arrays begin with 0.
$users_choice_array = explode("|", $users_choice);
// Count the number of values in array.
$users_choice_array_total = count($users_choice_array);
echo "<strong>Total User Choices:</strong> $users_choice_array_total <br />";
echo ("<strong>User's Choice Array:</strong><br />");
$a = 0;
for (; ; ) {
	if ($a > "$users_choice_array_total" -1) {
		break;
	}
	echo ("<strong>Result0:</strong> $users_choice_array[$a]<br />");
	$a++;
}
echo ("<hr />");
//-------------------------------------------------------------------------------------------------------
// Explode user's choice into an array.
// Always remember that arrays begin with 0.
$possible_choice_array = explode("|", $possible_choice);
// Count the number of values in array.
$possible_choice_array_total = count($possible_choice_array);
echo "<strong>Total Possible Choices:</strong> $possible_choice_array_total <br />";
echo ("<strong>Possible Choice Array:</strong><br />");
$b = 0;
for (; ; ) {
	if ($b > "$possible_choice_array_total" -1) {
		break;
	}
	echo ("<strong>Result1:</strong> $possible_choice_array[$b]<br />");
	$b++;
}
echo ("<hr />");
//-------------------------------------------------------------------------------------------------------
?>
<font color="#FF0000">- START TABLE -</font><br />
<?
echo ("<strong>TRUE / FALSE ARRAY</strong><br />");
$c = 0;
$d = 0;
for (; ; ) {
	if ($c > "$possible_choice_array_total" -1) {
		break;
	}
	$outcome = array_search("$users_choice_array[$c]", $possible_choice_array);
	if (empty($outcome)) {
		$outcome_result = "False";
	}
	echo "$outcome ($outcome_result) <br> ";
	$c++;
}
//-------------------------------------------------------------------------------------------------------
?>
<font color="#FF0000">- END TABLE -</font>


<?php

$a="a,b,c,d,e,f";
$b="c,u,p,w,a,m";

$a=explode(',',$a);

$b=explode(',',$b);

$result = array_intersect($a, $b);


print_R($result);


?>

 

 

result:

 

Array

(

    [0] => a

    [2] => c

)

 

For those who want to see what the echo value is.

<?php

$a="a,b,c,d,e,f";
$b="c,u,p,w,a,m";

$a=explode(',',$a);

$b=explode(',',$b);

$result = array_intersect($a, $b);

$resulted=implode('<br>',$result);

echo " This is the result, of the matching values from the array of a and b:\n <br>$resulted";


?>

Have an example of how I'd use foreach and inarray? I tried to do in_array checks but it would still make 0 = empty, when it isnt, cuse that is the value "0" when something returned as "" it would be empty, "0" isnt, hmmm...

 

I tried doing this from the above users post, but it still didnt work, it kept saying "is true" when it isnt true.

 

	echo ("<strong>TRUE / FALSE ARRAY</strong><br />");
$outcome = array_intersect($users_choice_array, $possible_choice_array);
$outcome_final = implode('<br />', $outcome);
$outcome_final_preg = implode(' ', $outcome);
echo "<br />(Outcome: <strong>$outcome</strong>)><br />";

$c = 0;
while ($c <= $possible_choice_array_total) {
	if (preg_match("/$users_choice_array[$c]/i", "$outcome_final_preg")) {
		echo "$users_choice_array[$c] - Returned True <br />";
	} else {
		echo "$users_choice_array[$c] - Returned False <br />";
	}
	$c++;	
}

echo "This is the result of the matching values from the array of [users_choice_array] and [possible_choice_array]: <br /><br />";
echo "$outcome_final <br />";

$users_choice = ("Kirin Osode|Hagun|Unji");
$possible_choice = ("Kirin Osode|Unji|Unsho|Hagun|Plastic Belt");
$users_choice_array = explode("|", $users_choice);
$possible_choice_array = explode("|", $possible_choice);

foreach($possible_choice_array AS $choice) {
if(in_array($choice,$users_choice_array)) {
  $result[$choice] = true;
} else {
  $result[$choice] = false;
}
}

var_dump($result);

ahh didnt see ur last reply Mchl but I had basically just figured out exactly same as what you wrote :D thank you so much, the foreach was something i was missing when i attempted my inarray last time, so i was doing it wrong.

 

Here is my final code which works:

<? echo ("<strong>TRUE / FALSE ARRAY</strong><br />");

$qwert = array_values($possible_choice_array);
$qwert2 = array_values($users_choice_array);

foreach ($qwert as $v) {
	if (in_array("$v", $qwert2)) {
		echo "<input name='$v' type='checkbox' value='$v' checked/> $v";
	} else {
		echo "<input name='$v' type='checkbox' value='$v' /> $v";
	}
}
//-------------------------------------------------------------------------------------------------------
echo "<br />";
?>

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.