[英]JSON data encoded are repeated
我有一個包含多個數據的數據庫,但是它們當然是唯一的。 這些數據在每個表telcoCall , telcoData , telcoSMS中 ,具體取決於其類。
然后,我使用json_encode
將這些數據合並為1個單個數組。 在telcoCall內部,數據放置在適當的位置。 但是, telcoData和telcoSMS很雜亂。 這些表中的數據正在復制。 看起來就是這樣
這是代碼:
<?PHP
include '../initialization.php';
$mysqli = @mysqli_connect($host, $username, $password, $db);
$query = 'SELECT t.*, c.*, d.*, s.* '.
'FROM telco t '.
'INNER JOIN telcoCall c ON t.telcoId = c.telcoId '.
'INNER JOIN telcoData d ON t.telcoId = d.telcoId '.
'INNER JOIN telcoSMS s ON t.telcoId = s.telcoId '.
'ORDER BY t.telcoName, c.callName, d.dataName, s.smsName';
//setup array to hold information
$telcos = array();
//setup holders for the different types so that we can filter out the data
$telcoId = 0;
$callId = 0;
$dataId = 0;
$smsId = 0;
//setup to hold our current index
$telcoIndex = -1;
$callIndex = -1;
$dataIndex = -1;
$smsIndex = -1;
if ($result = mysqli_query($mysqli, $query)) {
//go through the rows
while($row = mysqli_fetch_assoc($result)) {
if($telcoId != $row['telcoId']) {
$telcoIndex++;
$callIndex = -1;
$dataIndex = -1;
$smsIndex = -1;
$telcoId = $row['telcoId'];
//add the console
$telcos[$telcoIndex]['Telco'] = $row['telcoName'];
//setup the information array
$telcos[$telcoIndex]['Call Promo'] = array();
$telcos[$telcoIndex]['Data Promo'] = array();
$telcos[$telcoIndex]['SMS Promo'] = array();
}
if($callId != $row['callId']) {
$callIndex++;
$callId = $row['callId'];
//add the model to the console
$telcos[$telcoIndex]['Call Promo'][$callIndex]['Call Name'] = $row['callName'];
//setup the title array
$telcos[$telcoIndex]['Call Promo'][$callIndex]['Call'] = array();
//add the game to the current console and model
$telcos[$telcoIndex]['Call Promo'][$callIndex]['Call'][] = array(
'Keyword' => $row['callKeyword'],
'Description' => $row['callDescription'],
'Number' => $row['callNumber'],
'Validity' => $row['callValidity'],
'Price' => $row['callPrice']
);
}
if($dataId != $row['dataId']) {
$dataIndex++;
$dataId = $row['dataId'];
//add the model to the console
$telcos[$telcoIndex]['Data Promo'][$dataIndex]['Data Name'] = $row['dataName'];
//setup the title array
$telcos[$telcoIndex]['Data Promo'][$dataIndex]['Data'] = array();
//add the game to the current console and model
$telcos[$telcoIndex]['Data Promo'][$dataIndex]['Data'][] = array(
'Keyword' => $row['dataKeyword'],
'Description' => $row['dataDescription'],
'Number' => $row['dataNumber'],
'Validity' => $row['dataValidity'],
'Volume' => $row['dataVolume'],
'Price' => $row['dataPrice']
);
}
if($smsId != $row['smsId']) {
$smsIndex++;
$smsId = $row['smsId'];
//add the model to the console
$telcos[$telcoIndex]['SMS Promo'][$smsIndex]['SMS Name'] = $row['smsName'];
//setup the title array
$telcos[$telcoIndex]['SMS Promo'][$smsIndex]['SMS'] = array();
//add the game to the current console and model
$telcos[$telcoIndex]['SMS Promo'][$smsIndex]['SMS'][] = array(
'Keyword' => $row['smsKeyword'],
'Description' => $row['smsDescription'],
'Number' => $row['smsNumber'],
'Validity' => $row['smsValidity'],
'Price' => $row['smsPrice']
);
}
}
mysqli_free_result($result);
}
echo json_encode($telcos);
mysqli_close($mysqli);
?>
我真的不知道為什么會這樣。
是的,斯科特是對的,那段代碼只是冰山一角,所以我完成了他的建議,即消除了多余的條目而留下的唯一條目。 https://gist.github.com/jwara/fdc805240cf03027ae20-代碼不是很干凈但是可以進行優化
看起來telcoId在telco表中是主要唯一的,但是在其他表中可以是多個(這很有意義),不幸的是,如果您嘗試將單(1)聯接到Multiple1(2)到Multiple2(2)聯接到Multiple3(3),它將創建1 * 2 * 2 * 3 = 12行,解釋了混亂的multi2和multi3。
因此,通過執行單獨的查詢,首先獲取電信公司信息,然后在telcoCall,telcoData,telcoSMS上為每個電信公司進行單獨的數據庫調用,您將發現效率較低,而不是進行單個數據庫調用。
或者,如果您只是想提高速度而又不關心干凈的代碼,則可以使用現在擁有的內容,但可以堆疊if條件:
編輯1:編輯修復邏輯缺陷,出於某種原因想到高斯消除
// Stacking order determined by your SQL ORDER BY: t.telcoName, c.callName, d.dataName, s.smsName
if($telcoId == $row['telcoId']) continue;
//telco stuff...
if($callId == $row['callId']) continue;
//call stuff...
if($dataId == $row['dataId']) continue;
//data stuff...
if($smsId == $row['smsId']) continue;
//sms stuff...
編輯2:另一種較慢的方法,需要較少的腦力勞動
if($telcoId != $row['telcoId']) {
//telco stuff...
callArrayTemp = array();
dataArrayTemp = array();
smsArrayTemp = array();
}
if(!in_array($row['callId'], callArrayTemp[])) {
callArrayTemp[] = $row['callId'];
//call stuff...
}
if(!in_array($row['dataId'], dataArrayTemp[])) {
dataArrayTemp[] = $row['dataId'];
//data stuff...
}
if(!in_array($row['smsId'], smsArrayTemp[])) {
smsArrayTemp[] = $row['smsId'];
//sms stuff...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.