循环并将关联数组中的键值存储在 PHP 中
Loop and store key value from associative array in PHP
我正在尝试从我从 SQL 查询
创建的 PHP 关联数组中提取值
SELECT WEEKDAY(Date) AS weekday, 100 * SUM(Att_Type) / COUNT(Att_Type) AS percentage
FROM Attendance
WHERE ID=$ID AND WEEK(Date) = WEEK(NOW())
GROUP BY WEEKDAY(Date)
存储在变量 $arr
中的内容是我使用 print_r
命令找到的内容:
Array ( [weekday] => 2 [percentage] => 100.0000 )
Array ( [weekday] => 1 [percentage] => 100.0000 )
Array ( [weekday] => 0 [percentage] => 66.6667 )
Array ( [weekday] => 3 [percentage] => 100.0000 )
我是 PHP 关联数组的新手,我想知道如何创建一个函数,它将获取工作日键的值,如果它是 0(星期一),存储值星期一变量中的百分比键等等,其余工作日值可用,最多 4(星期五)。
编辑:
我正在寻找的输出是将值保存到自变量的位置,因此在调用时它们会清楚地显示值,例如:
$monday = 66.6667
$tuesday = 100.0000
$wednesday = 100.0000
$thursday = 100.0000
$friday = 0 //when there is no value corresponding to that weekday in the array then it will be 0
所以它们可以稍后在代码中回显。
$data = [['weekday' => 2, 'percentage' => 100.0000,], ['weekday' => 1, 'percentage' => 100.0000,], ['weekday' => 0, 'percentage' => 66.6667,], ['weekday' => 3, 'percentage' => 100.0000,]];
$days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
foreach ($days as $day) {
${$day} = 0;
}
如果您使用 $results = mysqli_fetch_all($result, MYSQLI_ASSOC)
获取结果数组,请使用此;
$results = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach($results as $arr) {
${$days[$arr['weekday']]} = $arr['percentage'];
// $arr['weekday'] gets the weekday of the array;
// $days[$arr['weekday']] gets the day index from the days array
// ${$days[$arr['weekday']]} then creates a variable with the particular weekday as the variable name
}
或者如果您正在使用 while ($arr = mysqli_fetch_assoc($result))
while ($arr = mysqli_fetch_assoc($result)) {
${$days[$arr['weekday']]} = $arr['percentage'];
}
我正在尝试从我从 SQL 查询
创建的 PHP 关联数组中提取值SELECT WEEKDAY(Date) AS weekday, 100 * SUM(Att_Type) / COUNT(Att_Type) AS percentage
FROM Attendance
WHERE ID=$ID AND WEEK(Date) = WEEK(NOW())
GROUP BY WEEKDAY(Date)
存储在变量 $arr
中的内容是我使用 print_r
命令找到的内容:
Array ( [weekday] => 2 [percentage] => 100.0000 )
Array ( [weekday] => 1 [percentage] => 100.0000 )
Array ( [weekday] => 0 [percentage] => 66.6667 )
Array ( [weekday] => 3 [percentage] => 100.0000 )
我是 PHP 关联数组的新手,我想知道如何创建一个函数,它将获取工作日键的值,如果它是 0(星期一),存储值星期一变量中的百分比键等等,其余工作日值可用,最多 4(星期五)。
编辑:
我正在寻找的输出是将值保存到自变量的位置,因此在调用时它们会清楚地显示值,例如:
$monday = 66.6667
$tuesday = 100.0000
$wednesday = 100.0000
$thursday = 100.0000
$friday = 0 //when there is no value corresponding to that weekday in the array then it will be 0
所以它们可以稍后在代码中回显。
$data = [['weekday' => 2, 'percentage' => 100.0000,], ['weekday' => 1, 'percentage' => 100.0000,], ['weekday' => 0, 'percentage' => 66.6667,], ['weekday' => 3, 'percentage' => 100.0000,]];
$days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
foreach ($days as $day) {
${$day} = 0;
}
如果您使用 $results = mysqli_fetch_all($result, MYSQLI_ASSOC)
获取结果数组,请使用此;
$results = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach($results as $arr) {
${$days[$arr['weekday']]} = $arr['percentage'];
// $arr['weekday'] gets the weekday of the array;
// $days[$arr['weekday']] gets the day index from the days array
// ${$days[$arr['weekday']]} then creates a variable with the particular weekday as the variable name
}
或者如果您正在使用 while ($arr = mysqli_fetch_assoc($result))
while ($arr = mysqli_fetch_assoc($result)) {
${$days[$arr['weekday']]} = $arr['percentage'];
}