[英]How to loop multidimensional array with condition and count it on another Wordpress page
我有一個Wordpress頁面的數據庫數組:
DEV-用戶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變量匹配,則計算它。 然后,頁面上將顯示總用戶數。
我相信這基本上就是你要找的東西。
$schoolID = 4522;
$studentTotal = 0;
for ($i = 0; $i < count($wb_users); $i++) {
if ($wb_users[$i]['SchoolID'] === $schoolID) {
$studentTotal++;
}
}
您需要1.遍歷主陣列中的數組。 2.將“SchoolID”鍵與學校ID變量進行比較。 3.匹配時增加計數器。
使用array_count_values
和array_column
怎么array_column
?
$counts = array_count_values((array_column($wb_users, 'SchoolID')));
print_r($counts);
輸出:
Array(
[4522] => 2
[4210] => 3
[4250] => 1
[4244] => 1
[4375] => 1
)
演示: https : //3v4l.org/hCvq5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.