猿问

如何使用条件循环多维数组并在另一个 Wordpress 页面上对其进行计数

我有一个来自 Wordpress 页面的数据库数组:


dev-user-20190424.php


<?php

$wb_users = array(

    array(


        "Username" => "Azmina",

        "Level" => "form-5",

        "School" => "KOLEJ D PATINGGI ABANG HJ ABDILLAH, SARAWAK",

        "SchoolID" => 4522,


    ),

    array(


        "Username" => "Gudako",

        "Level" => "form-5",

        "School" => "KOLEJ D PATINGGI ABANG HJ ABDILLAH, SARAWAK",

        "SchoolID" => 4522,


    ),

        array(


        "Username" => "Takatosaijo29",

        "Level" => "form-5",

        "School" => "SEKOLAH MENENGAH KEBANGSAAN BAHANG, SABAH",

        "SchoolID" => 4210,


    ),

    array(


        "Username" => "Obon",

        "Level" => "form-5",

        "School" => "SEKOLAH MENENGAH KEBANGSAAN BAHANG, SABAH",

        "SchoolID" => 4210,


    ),

    array(


        "Username" => "Rayana12",

        "Level" => "form-5",

        "School" => "SEKOLAH MENENGAH KEBANGSAAN BAHANG, SABAH",

        "SchoolID" => 4210,


    ),

    array(


        "Username" => "kaiyuanbeh",

        "Level" => "form-5",

        "School" => "SEKOLAH MENENGAH KEBANGSAAN PERMATANG RAWA, PULAU PINANG",

        "SchoolID" => 4250,


    ),

    array(


        "Username" => "Divyabharthi",

        "Level" => "form-5",

        "School" => "SEKOLAH MENENGAH KEBANGSAAN PETALING, WP KL",

        "SchoolID" => 4244,

    ),

    array(


        "Username" => "blackchocolatecake",

        "Level" => "form-4",

        "School" => "SEKOLAH MENENGAH KEBANGSAAN POI LAM, PERAK",

        "SchoolID" => 4375,


    ),

    );

所以我想要实现的是通过循环数组 $wb-users 来显示另一个 WordPress 页面上的用户总数,如果 SchoolID 与当前用户学校 id 变量匹配,则对其进行计数。然后页面上会显示用户总数。


回首忆惘然
浏览 126回答 2
2回答

牧羊人nacy

我相信这基本上就是你要找的。$schoolID = 4522;$studentTotal = 0;for ($i = 0; $i < count($wb_users); $i++) {&nbsp; &nbsp; if ($wb_users[$i]['SchoolID'] === $schoolID) {&nbsp; &nbsp; &nbsp; &nbsp; $studentTotal++;&nbsp; &nbsp; }}您需要 1. 循环遍历主数组中的数组。2. 将“SchoolID”键与您的学校 ID 变量进行比较。3. 匹配时增加计数器。
随时随地看视频慕课网APP
我要回答