PHP在多个数组中计算相似值
我在数据库中有如下值:PHP在多个数组中计算相似值,php,arrays,Php,Arrays,我在数据库中有如下值: Row 1 : ["2","3"] Row 2 : ["1","3"] Row 3 : ["2","3","4"] Array ( [0] => 2 [1] => 3 ) Array ( [0] => 1 [1] => 3 ) Array ( [0] => 2 [1] => 3 [2] => 4 ) 在前端,我选择了所有行,现在我想显示类似值的计数。 例如:上述代码中的所需o/p:1=1、2=2、3=3、4=1的计数 当我对
Row 1 : ["2","3"]
Row 2 : ["1","3"]
Row 3 : ["2","3","4"]
Array ( [0] => 2 [1] => 3 )
Array ( [0] => 1 [1] => 3 )
Array ( [0] => 2 [1] => 3 [2] => 4 )
在前端,我选择了所有行,现在我想显示类似值的计数。
例如:上述代码中的所需o/p:1=1、2=2、3=3、4=1的计数
当我对上述值进行json_解码并使用print_r时,我得到如下结果:
Row 1 : ["2","3"]
Row 2 : ["1","3"]
Row 3 : ["2","3","4"]
Array ( [0] => 2 [1] => 3 )
Array ( [0] => 1 [1] => 3 )
Array ( [0] => 2 [1] => 3 [2] => 4 )
注意:行列表可以增加,如何找到类似的值
我尝试了如图所示的数组_intersect,但没有成功
例如:这里的图像
获取上述数据的代码:
$conn = new mysqli("localhost", "root", "", "ams");
$query="SELECT * FROM attendance WHERE subject = '$subj'";
$result = $conn->query($query);
<table class="table table-responsive">
<tr>
<th>Sr. No</th>
<th>Col 1 </th>
<th>Col 2</th>
</tr>
<form method="post">
<?php
$i=1;
if (mysqli_num_rows($result) > 0) {
while ($row=mysqli_fetch_assoc($result)) {
$data = $row['att'];
$data = json_decode($data);
echo "<tr>";
echo "<td>" . $i . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . print_r($data) . "</td>";
echo "</tr>";
$i++;
}
}
?>
</form>
</table>
$conn=newmysqli(“localhost”、“root”、“ams”);
$query=“从主题为“$subj”的考勤中选择*;
$result=$conn->query($query);
高级工程师
第1列
第2列
所以,我为你上了这门课。它将在初始化时创建连接
class RowData {
private $connection;
private $returnContent = array();
private $stmt = null;
function __construct() {
$connection = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DB, MYSQL_USERNAME, MYSQL_PASSWORD);
$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection = $connection;
}
public function get($subj) {
$this->getContentFromDB($subj);
$this->parseContent();
return $this->returnContent;
}
private function getContentFromDB($subj) {
$stmt = $this->connection->prepare("SELECT * FROM attendance WHERE subject = '{$subj}'");
$stmt->execute();
$this->stmt = $stmt;
}
private function parseContent() {
$content = $stmt->fetchAll(PDO::FETCH_OBJ);
if(count($content) < 1) {
throw new Exception('Unable to find any attendies');
}
foreach($content as $values) {
$row = $this->getJsonArray($values->att);
$this->findValues($row);
}
}
private function getJsonArray($content) {
return json_decode($content);
}
private function findValues(array $row) {
foreach($row as $key => $value) {
if(isset($this->returnContent[$value])) {
$this->returnContent[$value] = $this->returnContent[$value] + 1;
} else {
$this->returnContent[$value] = 1;
}
}
return;
}
}
要使用该类,您可以编写如下内容:
$rowData = new RowData();
try {
$content = $rowData->get();
} catch (Exception $e) {
// No results were found
}
希望这有帮助!如果你需要帮助实现这一点,让我知道,我会非常乐意帮助你 所以,我给你上了这门课。它将在初始化时创建连接
class RowData {
private $connection;
private $returnContent = array();
private $stmt = null;
function __construct() {
$connection = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DB, MYSQL_USERNAME, MYSQL_PASSWORD);
$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection = $connection;
}
public function get($subj) {
$this->getContentFromDB($subj);
$this->parseContent();
return $this->returnContent;
}
private function getContentFromDB($subj) {
$stmt = $this->connection->prepare("SELECT * FROM attendance WHERE subject = '{$subj}'");
$stmt->execute();
$this->stmt = $stmt;
}
private function parseContent() {
$content = $stmt->fetchAll(PDO::FETCH_OBJ);
if(count($content) < 1) {
throw new Exception('Unable to find any attendies');
}
foreach($content as $values) {
$row = $this->getJsonArray($values->att);
$this->findValues($row);
}
}
private function getJsonArray($content) {
return json_decode($content);
}
private function findValues(array $row) {
foreach($row as $key => $value) {
if(isset($this->returnContent[$value])) {
$this->returnContent[$value] = $this->returnContent[$value] + 1;
} else {
$this->returnContent[$value] = 1;
}
}
return;
}
}
要使用该类,您可以编写如下内容:
$rowData = new RowData();
try {
$content = $rowData->get();
} catch (Exception $e) {
// No results were found
}
希望这有帮助!如果你需要帮助实现这一点,让我知道,我会非常乐意帮助你 我很困惑。你用的是什么数据库?我用的是MYSQL数据库你不应该得到这样的回应。您使用什么驱动程序连接到Mysql?你能在这个问题上给出一些代码吗。这很模糊。我可以用PDO给你一个答案吗?我不太清楚如何利用
mysqli\ucode>?@McStuffins:是的,没有问题,我很困惑。你用的是什么数据库?我用的是MYSQL数据库你不应该得到这样的回应。您使用什么驱动程序连接到Mysql?你能在这个问题上给出一些代码吗。这很模糊。我可以用PDO给你一个答案吗?我不太清楚如何利用mysqli\ucode>?@McStuffins:是的,没有问题