亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jquery - JSON 對象,格式為 <optgroup>,由 PHP 和 MySQL 的

jquery - JSON 對象,格式為 <optgroup>,由 PHP 和 MySQL 的

PHP
慕工程0101907 2022-07-02 15:46:26
我花了一整天的時間嘗試制作一個 PHP 腳本,將 MySQL 查詢結果轉換為<optgroup>Select2 中的 JSON 對象,當我終于找到解決方案時,它不起作用(Select2 下拉菜單中沒有任何內容)。我只是整個編程的初學者,我真的不知道如何解決這個問題。Stack 上的其他答案并沒有真正幫助我。這是我到目前為止得到的:PHP$gardinersClasses = array(    "A. Man and his Occupations",    "B. Woman and her Occupations",    "C. Anthropomorphic Deities",    "D. Parts of the Human Body",    ...etc.);$results = array();$data = array();$rowNumber = mysqli_num_rows($fetchData);if ($rowNumber > 0) {    while ($row = mysqli_fetch_array($fetchData, MYSQLI_ASSOC)) {        $results[] = array(            "id" => $row["id"],            "text" => $row["hieroglyph_hieroglyphica_code"],            "class" => $row["gardiners_class_id"]        );    }    usort($results, function($a, $b) {        $retVal = $a["class"] <=> $b["class"];        if ($retVal == 0) {            $retVal = strnatcmp($a["text"], $b["text"]);        }        return $retVal;    });    foreach($results as $result) {        $gardinersClassNumber = $result["class"]-1;        $gardinersClass = $gardinersClasses[$gardinersClassNumber];        if (empty($data)) {            $data[$gardinersClassNumber] = array(                "text" => $gardinersClass,                "children" => array(array(                        "id" => $result["id"],                        "text" => $result["text"]                    )                )            );        } else {            $counter = 0;            foreach ($data as $subdata) {                if (in_array($gardinersClass, $subdata)) {                    $counter++;                }            }            if ($counter < 1) {                $data[$gardinersClassNumber] = array(                    "text" => $gardinersClass,                    "children" => array(array(                            "id" => $result["id"],                            "text" => $result["text"]                        )                    )                ); 
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

我無意中找到了解決方案。問題key出在$data故意用$gardinersClassNumber. 它應該是自動生成的。


這是正確的解決方案:


foreach($results as $result) {

    $gardinersClassNumber = $result["class"]-1;

    $gardinersClass = $gardinersClasses[$gardinersClassNumber];

    if (empty($data)) {

        $data[] = array(

            "text" => $gardinersClass,

            "children" => array(array(

                    "id" => $result["id"],

                    "text" => $result["text"]

                )

            )

        );

    } else {

        $counter = 0;

        foreach ($data as $subdata) {

            if (in_array($gardinersClass, $subdata)) {

                $counter++;

            }

        }

        if ($counter < 1) {

            $data[] = array(

                "text" => $gardinersClass,

                "children" => array(array(

                        "id" => $result["id"],

                        "text" => $result["text"]

                    )

                )

            );

        } else {

            $currentKey = key($data);

            $data[$currentKey]["children"][] = array(

                "id" => $result["id"],

                "text" => $result["text"]

            );

        }

    }

}

如您所見,key()當在數組中找到匹配項時,我使用函數檢索當前鍵,然后我使用它在該位置插入數據。


查看完整回答
反對 回復 2022-07-02
  • 1 回答
  • 0 關注
  • 100 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號