简体   繁体   English

php遍历多个数组

[英]php looping through multiple arrays

I have a form that has 7 different criteria. 我的表格有7个不同的条件。 Each criteria has 3 or 4 questions that a user will check the appropriate value. 每个条件都有3或4个问题,用户将检查相应的值。

I want to loop through each question and create a string which contains the criteria id -> question id -> and answer value. 我想遍历每个问题,并创建一个包含条件ID->问题ID->和答案值的字符串。

I cannot seem to get it to work. 我似乎无法使其正常工作。

Here is a dump of the arrays: 这是数组的转储:

criteria array: 条件数组:

array(7) { [0]=> string(1) "1" [1]=> string(1) "3" [2]=> string(1) "5" [3]=> string(1) "7" [4]=> string(2) "10" [5]=> string(2) "11" [6]=> string(2) "12" } 

criteria id associated with each question 与每个问题相关的标准ID

array(19) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "3" [4]=> string(1) "3" [5]=> string(1) "3" [6]=> string(1) "5" [7]=> string(1) "5" [8]=> string(1) "5" [9]=> string(1) "7" [10]=> string(1) "7" [11]=> string(1) "7" [12]=> string(1) "7" [13]=> string(2) "10" [14]=> string(2) "10" [15]=> string(2) "10" [16]=> string(2) "11" [17]=> string(2) "11" [18]=> string(2) "11" } 

Question and answer array(only answered 6 question) 问答数组(仅回答6个问题)

array(6) { [11]=> array(1) { [0]=> string(1) "5" } [12]=> array(1) { [0]=> string(1) "5" } [13]=> array(1) { [0]=> string(1) "5" } [14]=> array(1) { [0]=> string(1) "4" } [15]=> array(1) { [0]=> string(1) "4" } [16]=> array(1) { [0]=> string(1) "4" } }

Here is my code for looping that doesn't seem to work: 这是我似乎没有用的循环代码:

                  $criteriaNums = $_POST['criteriaNum'];
              $fk_criterias = $_POST['fk_criteria'];
              $questionValue = $_POST['questionValue'];

              foreach($questionValue as $question => $answer){
                //echo $question;
                    foreach($fk_criterias as $fk){
                        foreach($criteriaNums as $crit){
                            if($fk == $crit){
                                $string1 = $fk;
                                //echo $string1;
                            }
                        }
                    }
                    $string2 = $question;
                    foreach ( $answer as $result ){
                      $string3 = $result;
                      //echo $string3;
                  }
                $criteria .= $string1 . '|' . $string2 . '|' . $string3 . '|';
                //echo $criteria;
              }

any help would be greatly appreciated!! 任何帮助将不胜感激!!

Strings 2 and 3 seem to be working fine. 字符串2和3似乎工作正常。 It is string1 that isn't working. 是string1不起作用。

this is the output the above code generates: 这是上面的代码生成的输出:

11|11|5|11|11|5|11|12|5|11|11|5|11|12|5|11|13|5|11|11|5|11|12|5|11|13|5|11|14|4|11|11|5|11|12|5|11|13|5|11|14|4|11|15|4|11|11|5|11|12|5|11|13|5|11|14|4|11|15|4|11|16|4| 11 | 11 | 5 | 11 | 11 | 5 | 11 | 12 | 5 | 11 | 11 | 5 | 11 | 12 | 5 | 11 | 13 | 5 | 11 | 11 | 5 | 11 | 12 | 5 | 11 | 13 | 5 | 11 | 14 | 4 | 11 | 11 | 5 | 11 | 12 | 5 | 11 | 13 | 5 | 11 | 14 | 4 | 11 | 15 | 4 | 11 | 11 | 5 | 11 | 12 | 5 | 11 | 13 | 5 | 11 | 14 | 4 | 11 | 15 | 4 | 11 | 16 | 4 |

This is how this should work: 这应该是这样的:

There is a total of 19 questions. 共有19个问题。 In the above question/answer array dump the user only answered 6. 在上述问题/答案数组转储中,用户仅回答了6。

The first question is id #11 and the answer is value 5. the second question is id #12 and the answer is value 5 third is id #13 answer is 5 4th is id #14 answer is 4 5th is id #15 answer is 4 6th is id #16 answer is 4 第一个问题是ID#11,答案是值5。第二个问题是ID#12,答案是值5第三是ID#13答案是5 4th是ID#14答案是4 5th是ID#15答案是4 6th是ID#16答案是4

The criteria id array dump above is the associated criteria that goes with each question. 上面的条件ID数组转储是每个问题附带的关联条件。 if you look at the dump you can see the first 6 (0 - 5) has 1, 1, 1, 3, 3, 3 如果您查看转储,您会看到前6个(0-5)具有1,1,1,3,3,3

so the expected output should be 所以预期的输出应该是

1 | 1 | 11 | 11 | 5 | 5 | 1 | 1 | 12 | 12 | 5 | 5 | 1 | 1 | 13 | 13 | 5 | 5 | 3 | 3 | 14 | 14 | 4 | 4 | 3 | 3 | 15 | 15 | 4 | 4 | 3 | 3 | 16 | 16 | 4 | 4 |

I hope this helps? 我希望这有帮助?

Array ( [1] => Array ( [11] => Array ( [0] => 5 ) 数组([1] =>数组([11] =>数组([0] => 5)

        [12] => Array
            (
                [0] => 5
            )

        [13] => Array
            (
                [0] => 5
            )

    )

[3] => Array
    (
        [14] => Array
            (
                [0] => 5
            )

        [15] => Array
            (
                [0] => 5
            )

        [16] => Array
            (
                [0] => 5
            )

    )

)

1|11|5|1|12|5|1|13|5|3|14|5|3|15|5|3|16|5| 1 | 11 | 5 | 1 | 12 | 5 | 1 | 13 | 5 | 3 | 14 | 5 | 3 | 15 | 5 | 3 | 16 | 5 |

Here is my code: 这是我的代码:

                foreach ($questionValue as $crit => $questions) {
                    foreach ($questions as $answer => $result) {
                        foreach($result as $value){
                            $criteria = $crit . "|" . $answer . "|" . $value . "|";
                            echo $criteria;
                        }
                    }
                }                 

Can you please provide output? 你能提供输出吗? Can you please provide an example of the input arrays? 您能否提供输入数组的示例? The part doing $string1 looks ok, without the aforementioned data it is doubful that the problem can be resolved. 执行$string1的部分看起来还不错,如果没有上述数据,可以解决该问题就令人怀疑。

This one looks strange: 这看起来很奇怪:

foreach ( $answer as $result ){
                      $string3 = $result;
                      //echo $string3;
                  }

You may either want to add a condition (like in criteria) or do $string3 .= $result; 您可能想要添加条件(如条件中的条件)或执行$string3 .= $result;

I fixed the issue: 我解决了这个问题:

I used concatenation for the values. 我使用串联的值。

Here is my code: 这是我的代码:

                foreach ($questionValue as $crit => $questions) {
                    foreach ($questions as $answer => $result) {
                        foreach($result as $value){
                            $string .= $crit . "|" . $answer . "|" . $value . "|";
                            //echo $criteria;
                        }
                    }
                  $criteria = $string;
                }                 

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

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