简体   繁体   English

重复编码的JSON数据

[英]JSON data encoded are repeated

I have a database with several data in it, but they are unique of course. 我有一个包含多个数据的数据库,但是它们当然是唯一的。 These data are inside each table, telcoCall , telcoData , telcoSMS , depending on its class. 这些数据在每个表telcoCalltelcoDatatelcoSMS中 ,具体取决于其类。

I then use json_encode to merge these data into 1 single array. 然后,我使用json_encode将这些数据合并为1个单个数组。 Inside telcoCall , the data are in proper place. telcoCall内部,数据放置在适当的位置。 However, telcoData and telcoSMS are messy. 但是, telcoDatatelcoSMS很杂乱。 The data inside these tables are being duplicated. 这些表中的数据正在复制。 This is how it looks, 看起来就是这样

在此处输入图片说明

Here's the code: 这是代码:

<?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);
?>

I really don't know why this is happening. 我真的不知道为什么会这样。

yep Scott was right, that snippet was just the tip of the ice berg, so I finish what he suggested eliminating the redundant entries leaving with unique ones. 是的,斯科特是对的,那段代码只是冰山一角,所以我完成了他的建议,即消除了多余的条目而留下的唯一条目。 https://gist.github.com/jwara/fdc805240cf03027ae20 -- code is kind of not clean but subject to optimizations https://gist.github.com/jwara/fdc805240cf03027ae20-代码不是很干净但是可以进行优化

http://cl.ly/image/2I3v2O29341J/o http://cl.ly/image/2I3v2O29341J/o

It looks telcoId is primary unique in telco table, but can be multiple in other tables (which makes sense), unfortunately if you try to join single (1) to multiple1 (2) to multiple2 (2) to multiple3 (3), it'd create 1 * 2 * 2 * 3 = 12 rows, which explains the messy multiple2 and multiple3. 看起来telcoId在telco表中是主要唯一的,但是在其他表中可以是多个(这很有意义),不幸的是,如果您尝试将单(1)联接到Multiple1(2)到Multiple2(2)联接到Multiple3(3),它将创建1 * 2 * 2 * 3 = 12行,解释了混乱的multi2和multi3。

So instead of doing that single DB call, you'll be looking at less efficiency by running separate queries, getting the telco info first, then making individual DB calls for each telco on telcoCall, telcoData, telcoSMS. 因此,通过执行单独的查询,首先获取电信公司信息,然后在telcoCall,telcoData,telcoSMS上为每个电信公司进行单独的数据库调用,您将发现效率较低,而不是进行单个数据库调用。

Or if you just want the speed and don't care about clean code, you can just use what you have right now, but stack up the if conditions: 或者,如果您只是想提高速度而又不关心干净的代码,则可以使用现在拥有的内容,但可以堆叠if条件:

EDIT 1: Edited to fix logic flaw, for some reason Gaussian elimination comes to mind 编辑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...

EDIT 2: Another slower way that requires less brainwork 编辑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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM