基本上,我从API获取数据,并且API具有重复的数字。我想计算 this.postCollection = this.afs.collection< post > ('posts')
中的重复数字,但由于某种原因它不起作用。我不确定我做错了什么。您可以多次查看foreach
次。查看图片如何打印。它应该显示类似的东西
33
代码:
Array
(
[33] => 5
[2] => 3
)
答案 0 :(得分:0)
您的代码的根本缺陷是您使用一个值并对其进行格式化,然后将其放入数组然后执行 $username = "";
$password = "";
if(!empty($_POST['username']) && !empty($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
}
$usr = "123";
$pwd = "123";
$invalidLogin = 'Incorrect Username and/or Password. Please try again.';
if ($password == $pwd && $username == $usr){
session_start();
$_SESSION['loggedin'] = true;
header('Location: memberPage.php');
}else{
echo '<script type="text/javascript">';
echo 'alert("Incorrect Username and/or Password. Please try again.");';
echo 'window.location.href = "memberLogin.html";';
echo '</script>';
}
就可以了。
看到区别:
<?php
session_start();
if ($_SESSION['loggedin'] != true){
header('memberLogin.html');
}
?>
<!DOCTYPE html>
<!--Rest of the HTML code--!>
首先打印array_count_values
:
$arr = [33, 32, 23, 33, 22, 23, 32, 33, 33];
print_r(array_count_values($arr));
foreach($arr as $a) {
$currentp= number_format($a, 6);
$array = array($currentp);
$vals = array_count_values($array);
print_r($vals);
}
print_r
内Array
(
[33] => 4
[32] => 2
[23] => 2
[22] => 1
)
打印:
print_r
你意识到差异吗?
我想,你需要的是
foreach
哪个输出:
Array([33.000000] => 1)
Array([32.000000] => 1)
Array([23.000000] => 1)
Array([33.000000] => 1)
Array([22.000000] => 1)
Array([23.000000] => 1)
Array([32.000000] => 1)
Array([33.000000] => 1)
Array([33.000000] => 1)
答案 1 :(得分:0)
不确定为什么使用number_format()
,但我猜你需要这样的东西
$num_array = []; // Initialize number array
foreach ($parsed_json1->numbers as $item) {
$ui = $item->no;
$currentp= number_format($ui, 6);
$num_array[] = $currentp; // Store each number first to number array
}
// After foreach loop, do array_count_values to number array
$vals = array_count_values($num_array);
print_r($vals); //Print result