如果使用foreach条件为真,如何在空白数组中获取数组数据

时间:2018-08-14 14:28:16

标签: php mysql codeigniter codeigniter-3 codeigniter-database

我有一个数组:

$stu_result( [exam_type] => 1 [subject_id] => 5 [converted_mark] =>5.00[student_id] => 186 [sub_name] => maths)

,长度为15。
我想匹配考试类型,并将sub_name和转换后的分数存储在空白数组中。我尝试了这段代码:

这是我到目前为止尝试的代码

$result_exam1 = array();
foreach($stu_result as $result_temp){
 if($result_temp['exam_type'] == 1){
            $result_exam1['converted_mark'] = $result_temp['converted_mark'];
            $result_exam1['sub_name'] = $result_temp['sub_name'];
        }
}

2 个答案:

答案 0 :(得分:2)

希望这对您有帮助:

注意:$stu_result应该是要循环的多维数组,并使用foreach循环的key value功能来获取所有数据,如下所示:

$result_exam1 = array();
foreach($stu_result as $key => $result_temp)
{
   if($result_temp['exam_type'] == 1)
   {
       $result_exam1[$key]['converted_mark'] = $result_temp['converted_mark'];
       $result_exam1[$key]['sub_name'] = $result_temp['sub_name'];
   }
}
print_r($result_exam1);

答案 1 :(得分:1)

我用$ stu_result刷新了整个数组。这是您要找的吗?

session_info("blogdown")

Session info ----------------------------------------------
 setting  value                       
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, mingw32             
 ui       RStudio (1.1.453)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/New_York            
 date     2018-08-14                  

Packages --------------------------------------------------
 package   * version  date       source        
 backports   1.1.2    2017-12-13 CRAN (R 3.5.0)
 base64enc   0.1-3    2015-07-28 CRAN (R 3.5.0)
 BH          1.66.0-1 2018-02-13 CRAN (R 3.5.0)
 blogdown    0.8      2018-07-15 CRAN (R 3.5.1)
 bookdown    0.7      2018-02-18 CRAN (R 3.5.1)
 digest      0.6.15   2018-01-28 CRAN (R 3.5.1)
 evaluate    0.11     2018-07-17 CRAN (R 3.5.1)
 glue        1.3.0    2018-07-17 CRAN (R 3.5.1)
 graphics  * 3.5.1    2018-07-02 local         
 grDevices * 3.5.1    2018-07-02 local         
 highr       0.7      2018-06-09 CRAN (R 3.5.1)
 htmltools   0.3.6    2017-04-28 CRAN (R 3.5.1)
 httpuv      1.4.5    2018-07-19 CRAN (R 3.5.1)
 jsonlite    1.5      2017-06-01 CRAN (R 3.5.1)
 knitr       1.20     2018-02-20 CRAN (R 3.5.1)
 later       0.7.3    2018-06-08 CRAN (R 3.5.1)
 magrittr    1.5      2014-11-22 CRAN (R 3.5.1)
 markdown    0.8      2017-04-20 CRAN (R 3.5.1)
 methods   * 3.5.1    2018-07-02 local         
 mime        0.5      2016-07-07 CRAN (R 3.5.0)
 promises    1.0.1    2018-04-13 CRAN (R 3.5.1)
 R6          2.2.2    2017-06-17 CRAN (R 3.5.1)
 Rcpp        0.12.18  2018-07-23 CRAN (R 3.5.1)
 rlang       0.2.1    2018-05-30 CRAN (R 3.5.1)
 rmarkdown   1.10     2018-06-11 CRAN (R 3.5.1)
 rprojroot   1.3-2    2018-01-03 CRAN (R 3.5.1)
 servr       0.10     2018-05-30 CRAN (R 3.5.1)
 stats     * 3.5.1    2018-07-02 local         
 stringi     1.1.7    2018-03-12 CRAN (R 3.5.0)
 stringr     1.3.1    2018-05-10 CRAN (R 3.5.1)
 tinytex     0.6      2018-07-07 CRAN (R 3.5.1)
 tools       3.5.1    2018-07-02 local         
 utils     * 3.5.1    2018-07-02 local         
 xfun        0.3      2018-07-06 CRAN (R 3.5.1)
 yaml        2.2.0    2018-07-25 CRAN (R 3.5.1)