कुछ इस तरह काम करना चाहिए:
<?php
$g = 'the weather is nice'; // strings to loop through
$n = 'the water is blue';
$b = 'that was a bad movie';
$t = 'hows the weather'; // example input
$test = (str_word_count($t, 1)); // breaks out each word into array
// Comparisons
$comps = array();
// Array sums
$sums = array();
// Search each variable that's been set, as long as it's less that 't'
// A "for" loop will accept letters in addition to numbers, so we'll start with the
// letter "a" and loop through each letter up to "s" (which is one less than "t")
for ($inc = 'a'; $inc < 't'; $inc++) {
// Now, a variable assigned as $$inc will translate into $a, $b, $c ... $s
// and if $a, $b, $c, etc, are set...
if (isset($$inc)) {
// ... assign them to the $comps array with a key of $$inc
$comps[$$inc] = str_word_count($$inc, 1);
// For example, when the "for" loop reaches "f", nothing will be added to the
// $comps array because $f is not set above.
// But when it gets to "g" it'll find that $g HAS been set, and that it has a
// value of "the weather is nice". At this point the $comps array will now look
// like this:
// $comps['the weather is nice'] = array('the', 'weather', 'is', 'nice');
// If you'd like to see this in action (since it might sound a little confusing),
// remove the # from the beginning of each of the following lines that start with #
// (there should be 10 total):
#print "<pre>The loop has reached the letter <b>{$inc}</b> for the value of ";
#print "<b>\$inc</b> and has found that <b>\${$inc}</b> HAS been set in the code.\n";
#print "Adding another dollar sign to <b>\$inc</b> has had the following effects:\n";
#print "- <b>\$inc</b> now looks like <b>\$\$inc</b> (from within the written part of the code)\n";
#print "- <b>\$\$inc</b> translates into <b>\${$inc}</b> (the variable that is acually being evaluated)\n";
#print "- <b>\${$inc}</b> evaluates to <b>{$$inc}</b>\n</pre>";
}
#else {
# print "<pre>The loop has reached the letter <b>{$inc}</b> for the value of <b>\$inc</b>";
# print " and has found that <b>\${$inc}</b> has NOT been set in the code, so it's being skipped.\n";
#}
}
// Avoid errors by checking if empty or not
if (!empty($comps)) {
foreach ($comps as $key => $comp) {
// Find intersections, if any
$candidates[$key] = array_intersect($test, $comp);
// Count the intersections
$counts[$key] = array_count_values($candidates[$key]);
// Add up the intersections
$sums[$key] = array_sum($counts[$key]);
}
}
$winner = '';
if (!empty($sums)) {
// Reverse sort $sums, putting the highest value first
arsort($sums);
// Flip $sums so we can extract the key
$flipped = array_flip($sums);
// Extract the first key off of $sums
$winner = array_shift($flipped);
}
print $winner;
धन्यवाद @revo यह बहुत अच्छा है! –
आपका स्वागत है। साथ ही जब आपने जेर्डडिजिटी के जवाब को स्वीकार किया है तो आपको उसे एक संशोधन करने के लिए कहा जाना चाहिए क्योंकि तारों के भीतर दोहराए गए शब्दों की संख्या उनके कोड में ध्यान नहीं दे रही है। @RyanD – revo
हां, जिस तरह से मैंने उसे जवाब दिया, मैंने नहीं देखा कि उन्हें कहां से लिया जा रहा था, सोचा था कि मुझे बस कुछ याद आ रहा था .. –