douzhushen_9776 2019-06-20 02:03
浏览 54

如何使用条件循环多维数组并将其计入另一个Wordpress页面

I have an array of database from a Wordpress page:

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,

    ),
    );

So what I am trying to achieve is to display the total number of users on another WordPress page by looping the array $wb-users and if SchoolID is matched current user school id variable, then count it. Then the total number of users will be displayed on the page.

  • 写回答

2条回答 默认 最新

  • dsoy71058 2019-06-20 02:43
    关注

    I believe this is basically what you are looking for.

    $schoolID = 4522;
    $studentTotal = 0;
    for ($i = 0; $i < count($wb_users); $i++) {
        if ($wb_users[$i]['SchoolID'] === $schoolID) {
            $studentTotal++;
        }
    }
    

    You need to 1. Loop through the arrays within the main array. 2. Compare the 'SchoolID' key with your school ID variable. 3. Increment the counter when there is a match.

    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏