viion Posted March 8, 2009 Share Posted March 8, 2009 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> Link to comment https://forums.phpfreaks.com/topic/148497-solved-need-help-on-making-a-truefalse-from-explodefors/ Share on other sites More sharing options...
redarrow Posted March 8, 2009 Share Posted March 8, 2009 <?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"; ?> Link to comment https://forums.phpfreaks.com/topic/148497-solved-need-help-on-making-a-truefalse-from-explodefors/#findComment-779801 Share on other sites More sharing options...
viion Posted March 8, 2009 Author Share Posted March 8, 2009 Umm, I don't really think array_intersect is what I am looking for, this doesn't return a true of false value on checking. I need something similar to SQL's "where" such and such = such and such. Link to comment https://forums.phpfreaks.com/topic/148497-solved-need-help-on-making-a-truefalse-from-explodefors/#findComment-779808 Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 Use foreach() and in_array Link to comment https://forums.phpfreaks.com/topic/148497-solved-need-help-on-making-a-truefalse-from-explodefors/#findComment-779823 Share on other sites More sharing options...
viion Posted March 8, 2009 Author Share Posted March 8, 2009 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 />"; Link to comment https://forums.phpfreaks.com/topic/148497-solved-need-help-on-making-a-truefalse-from-explodefors/#findComment-779828 Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 $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); Link to comment https://forums.phpfreaks.com/topic/148497-solved-need-help-on-making-a-truefalse-from-explodefors/#findComment-779839 Share on other sites More sharing options...
viion Posted March 8, 2009 Author Share Posted March 8, 2009 ahh didnt see ur last reply Mchl but I had basically just figured out exactly same as what you wrote 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 />"; ?> Link to comment https://forums.phpfreaks.com/topic/148497-solved-need-help-on-making-a-truefalse-from-explodefors/#findComment-779847 Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 Nice BTW: Do not use short tags ( <? ). They're deprecated, cause problems when working with XML and are disabled by default in PHP5. Use <?php instead Link to comment https://forums.phpfreaks.com/topic/148497-solved-need-help-on-making-a-truefalse-from-explodefors/#findComment-779877 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.