jquery - JSON 对象,格式为 <optgroup>,由 PHP 和 MySQL 的

我花了一整天的时间尝试制作一个 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"]

                        )

                    )

                );

 

慕工程0101907
浏览 120回答 1
1回答

慕桂英546537

我无意中找到了解决方案。问题key出在$data故意用$gardinersClassNumber. 它应该是自动生成的。这是正确的解决方案:foreach($results as $result) {&nbsp; &nbsp; $gardinersClassNumber = $result["class"]-1;&nbsp; &nbsp; $gardinersClass = $gardinersClasses[$gardinersClassNumber];&nbsp; &nbsp; if (empty($data)) {&nbsp; &nbsp; &nbsp; &nbsp; $data[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "text" => $gardinersClass,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children" => array(array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => $result["id"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "text" => $result["text"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $counter = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($data as $subdata) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (in_array($gardinersClass, $subdata)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ($counter < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "text" => $gardinersClass,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children" => array(array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => $result["id"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "text" => $result["text"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentKey = key($data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data[$currentKey]["children"][] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => $result["id"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "text" => $result["text"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}如您所见,key()当在数组中找到匹配项时,我使用函数检索当前键,然后我使用它在该位置插入数据。
打开App,查看更多内容
随时随地看视频慕课网APP