【问题标题】:PHP associative Array get Array Id [duplicate]PHP关联数组获取数组ID [重复]
【发布时间】:2019-09-11 13:10:12
【问题描述】:

嗨,我无法理解这个我有以下数组:

Array
(
[questions] => Array
    (
        [585] => Array
            (
                [correct] => 1
                [mark] => 1
                [type] => single_choice
                [answered] => 1
            )

        [596] => Array
            (
                [correct] => 
                [mark] => 0
                [type] => true_or_false
                [answered] => 1
            )

        [595] => Array
            (
                [correct] => 1
                [mark] => 1
                [type] => single_choice
                [answered] => 1
            )

   )

)

我正在尝试在 foreach 语句中获取数组编号,这是我的代码,它适用于除我只需要在 foreach 外观中获得 585,596 ir 595 的数字之外的所有其他内容。

          <?php

               /// $quiz_res is the array 


                foreach($quiz_res['questions'] as $result) {

                      echo key($result); //// DOES NOT WORK 
                      #####  I need to get the number here eg 585 ???

                      echo $result['correct']; /// this works 
                      echo $result['mark']; /// this works 
                      echo $result['type']; /// this works  
                      echo $result['answered']; /// this works 
                }
           ?>

这也无关紧要,但这与learnpress测验的结果有关,如果有人熟悉它们的话。

任何帮助或指针将不胜感激

【问题讨论】:

    标签: php wordpress learnpress


    【解决方案1】:

    你必须在 foreach 调用中命名索引:

    foreach($quiz_res['questions'] as $id =&gt; $result) {

    echo $id; // 585

    【讨论】:

    • 非常感谢!!!我已经用那个敲了好几个小时了!
    猜你喜欢
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2015-06-26
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    相关资源
    最近更新 更多