简体   繁体   English

两个数组的所有组合PHP

[英]All combinations of two arrays PHP

I am reposting this, reworded, as the initial question didn't make much sense written down. 我将其重新发布,改写成文字,因为最初的问题写下来并没有多大意义。

I am trying to write a little piece of code that will add maths questions to a database. 我正在尝试编写一些代码,将数学问题添加到数据库中。 These ones are ratio questions. 这些是比率问题。

For instance I want to add lots of questions like 'Simplify 10: 6' and store the answer as '5 : 3'. 例如,我想添加很多问题,例如“简化10:6”,并将答案存储为“ 5:3”。

I was thinking the best thing to do would be store the digits as an array, and the early primes as an array (I only want prime multiples for now). 我当时认为最好的办法是将数字存储为数组,将早期素数存储为数组(我现在只想要素数倍数)。

I could then loop though this array. 然后,我可以循环通过此数组。 This I can do. 我能做的 However, I want every single possible combination of my digits, not just to loop through the array once. 但是,我希望数字的每个可能的组合,而不仅仅是循环遍历数组一次。

For instance I want the questions 2 : 4 , 3: 6, 5 : 10, 7: 14 etc etc 例如我想问2:4,3:6,5:10,7:14等

So I want to pair digits[0...8] with every other digit, and then loop though the primes multiplying these combinations with every prime. 所以我想将数字[0 ... 8]与其他每个数字配对,然后循环使用素数将这些组合与每个素数相乘。 I get that this would probably be some kind of nested for loop, but i've not really got much of an idea where to start. 我知道这可能是某种嵌套的for循环,但是我真的不知道从哪里开始。

This is what I have so far. 到目前为止,这就是我所拥有的。 It does exactly nothing. 它什么也没做。 I realise it would have been quicker to manually input all there but I want to learn a bit of coding, too. 我意识到手动输入所有内容会更快,但我也想学习一些编码。

   $digits = array(9,8,7,6,5,4,3,2,1);
   $primes = array(2,3,5,7,11);

   for ($first = 0; $first < count($digits)-1; $first++) {
         $num1 = $digits[$first];
      for ($second = 1; $second < count($digits)-2; $second++) {
         $num2 = $digits[$second];
            for ($third = 0; $third < count($primes)-1; $third++) {
         $num3 = $num1 * $primes[$third];
         $num4 = $num2 * $primes[$third];

         $questionlead = "Simplify <br>" .$num3. " : " . $num4 ;
         $ans = $num1. " : " . $num2 ;

   $dbhost = 'nope';
   $dbuser = 'notfor';
   $dbpass = 'you';

   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   $sql = "INSERT INTO QuestionDB(TopicID, TopicName, SubtopicID, SubtopicName, Question, Answer, Difficulty, Author, Projectable) VALUES (6,'Ratio',1,'Simplifying ratio','".$questionlead. "','".$ans. "',1,'Richard Tock','Yes')";
   mysql_select_db('db_name');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

      }
}
}
   mysql_close($conn);
?>

I'm lost at sea here 我在海上迷路了

This first function is the "smart" part because it does all the arithmetic. 第一个功能是“智能”部分,因为它执行所有算术运算。 It does not do simplifying after the fact, so if you end up finding an answer in the loop, it may be worth checking out if 1 is divided by 1, then you could just return 1. But I wanted to keep it this way depending on how you wanted to use it! 它并不能简化事实,因此,如果您最终在循环中找到答案,则可能值得检查一下是否将1除以1,然后可以只返回1。但是我想保留这种方式,具体取决于关于如何使用它!

function simplify(Int $num, Int $denom, Array $primes){
    //going from high primes to low primes
    foreach(array_reverse($primes) as $prime) {
        if (($num % $prime == 0) && ($denom % $prime == 0)) {
            if ($num % $denom == 0) {
                return [intdiv($num,$denom)];
            } elseif ($denom % $num == 0) {
                return [1,intdiv($denom,$num)];
            }
            return [$num / $prime,$denom / $prime];
        }
    }
    return [$num,$denom];
}

This next function does the looping through 2 arrays of numbers. 下一个函数执行2个数字数组的循环。 It is fairly ugly, I don't usually like nested for loops. 这非常丑陋,我通常不喜欢嵌套循环。

function simplifyArrays(Array $arr1, Array $arr2, Array $primes) {
    for($i=0; $i<count($arr1); $i++){
        $num1 = $arr1[$i];
        for($j=0; $j<count($arr2); $j++){
            $num2 = $arr2[$j];
            echo "simplify: $num1 / $num2 = " . implode("/",simplify($num1,$num2,$primes)) . "\n";
        }
    }
}

And to see it in action: https://3v4l.org/qmgJX 并查看实际效果: https//3v4l.org/qmgJX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM