V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Test2g
V2EX  ›  问与答

PHP 中我对数组替换的代码错在哪儿?

  •  
  •   Test2g · 2019-04-05 13:13:15 +08:00 · 792 次点击
    这是一个创建于 2060 天前的主题,其中的信息可能已经有所发展或是发生改变。

    接上一个提问,已经想出来排序的方法了——

    原始 js (下列代码前已经转为数组)部分如下——

    "xnxqid": "2018-2019-2",
        "data": [
          {
            "jxl": "河东校区-综合",
            "jsList": [
              {
                "jsid": "489",
                "jzwid": "01",
                "jsmc": "网络课程 1",
                "zws": 3000,
                "xqmc": "河东校区",
                "jsh": "26102",
                "jzwmc": "综合",
                "yxzws": 3000
              }
            ]
          },
          {
            "jxl": "河西校区-体育",
            "jsList": [
              {
                "jsid": "185",
                "jzwid": "17",
                "jsmc": "冶金篮球场-3",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "29991",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "186",
                "jzwid": "17",
                "jsmc": "冶金篮球场-4",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "29992",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "189",
                "jzwid": "17",
                "jsmc": "五食堂篮球场-1",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "29995",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "190",
                "jzwid": "17",
                "jsmc": "五食堂篮球场-2",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "29996",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "191",
                "jzwid": "17",
                "jsmc": "五食堂篮球场-3",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "29997",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "192",
                "jzwid": "17",
                "jsmc": "五食堂篮球场-4",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "29998",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "215",
                "jzwid": "17",
                "jsmc": "东门篮球场-8",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "29984",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "271",
                "jzwid": "17",
                "jsmc": "形体房 01",
                "zws": 100,
                "xqmc": "河西校区",
                "jsh": "29903",
                "jzwmc": "体育",
                "yxzws": 100
              },
              {
                "jsid": "272",
                "jzwid": "17",
                "jsmc": "形体房 02",
                "zws": 100,
                "xqmc": "河西校区",
                "jsh": "29904",
                "jzwmc": "体育",
                "yxzws": 100
              },
              {
                "jsid": "278",
                "jzwid": "17",
                "jsmc": "网络课程",
                "zws": 1000,
                "xqmc": "河西校区",
                "jsh": "29102",
                "jzwmc": "体育",
                "yxzws": 1000
              },
              {
                "jsid": "8D99F48DA7F249D1BC9AFB5182E80319",
                "jzwid": "17",
                "jsmc": "五食堂篮球场-6",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "17001",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "DE54DE36A2F44936AF53598AFFB7BA39",
                "jzwid": "17",
                "jsmc": "冶金足球场-1",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "17003",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "7D55C16A86E446489E3E9BE4E80FA846",
                "jzwid": "17",
                "jsmc": "东门足球场-2",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "17004",
                "jzwmc": "体育",
                "yxzws": 50
              },
              {
                "jsid": "452",
                "jzwid": "17",
                "jsmc": "实习场地(二校区)1",
                "zws": 210,
                "xqmc": "河西校区",
                "jsh": "29100",
                "jzwmc": "体育",
                "yxzws": 210
              },
              {
                "jsid": "455",
                "jzwid": "17",
                "jsmc": "实习场地(二校区)",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "29101",
                "jzwmc": "体育",
                "yxzws": 50
              }
            ]
          },
          {
            "jxl": "河西校区-计通",
            "jsList": [
              {
                "jsid": "273",
                "jzwid": "19",
                "jsmc": "计通楼 603",
                "zws": 183,
                "xqmc": "河西校区",
                "jsh": "30807",
                "jzwmc": "计通",
                "yxzws": 183
              },
              {
                "jsid": "482",
                "jzwid": "19",
                "jsmc": "计通楼 208",
                "zws": 80,
                "xqmc": "河西校区",
                "jsh": "30812",
                "jzwmc": "计通",
                "yxzws": 80
              },
              {
                "jsid": "551",
                "jzwid": "19",
                "jsmc": "计通楼 526",
                "zws": 183,
                "xqmc": "河西校区",
                "jsh": "30806",
                "jzwmc": "计通",
                "yxzws": 183
              }
            ]
          },
          {
            "jxl": "河西校区-电气",
            "jsList": [
              {
                "jsid": "413",
                "jzwid": "18",
                "jsmc": "电气楼 508",
                "zws": 136,
                "xqmc": "河西校区",
                "jsh": "30110",
                "jzwmc": "电气",
                "yxzws": 136
              }
            ]
          },
          {
            "jxl": "河西校区-外语",
            "jsList": [
              {
                "jsid": "86",
                "jzwid": "05",
                "jsmc": "外语楼 106",
                "zws": 45,
                "xqmc": "河西校区",
                "jsh": "30631",
                "jzwmc": "外语",
                "yxzws": 45
              },
              {
                "jsid": "570",
                "jzwid": "05",
                "jsmc": "外语楼 212",
                "zws": 40,
                "xqmc": "河西校区",
                "jsh": "30629",
                "jzwmc": "外语",
                "yxzws": 40
              },
              {
                "jsid": "620",
                "jzwid": "05",
                "jsmc": "外语楼 309",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "30616",
                "jzwmc": "外语",
                "yxzws": 50
              },
              {
                "jsid": "461",
                "jzwid": "05",
                "jsmc": "外语楼 310",
                "zws": 50,
                "xqmc": "河西校区",
                "jsh": "30617",
                "jzwmc": "外语",
                "yxzws": 50
              },
              {
                "jsid": "467",
                "jzwid": "05",
                "jsmc": "外语楼 401",
                "zws": 40,
                "xqmc": "河西校区",
                "jsh": "30623",
                "jzwmc": "外语",
                "yxzws": 40
              },
              {
                "jsid": "470",
                "jzwid": "05",
                "jsmc": "外语楼 402",
                "zws": 40,
                "xqmc": "河西校区",
                "jsh": "30624",
                "jzwmc": "外语",
                "yxzws": 40
              },
              {
                "jsid": "471",
                "jzwid": "05",
                "jsmc": "外语楼 403",
                "zws": 40,
                "xqmc": "河西校区",
                "jsh": "30625",
                "jzwmc": "外语",
                "yxzws": 40
              }
            ]
          },
    

    我的代码如下——

      $areas = $data['data'];
      foreach ($areas as $key => $value) {
        $classes = $value['jsList'];
        usort(
          $classes, 
          function($a, $b) { 
            if ($a['jsmc'] == $b['jsmc']) {
              return 0;
            }
              return ($a['jsmc'] < $b['jsmc']) ? -1 : 1;
          }
        );
        array_replace($value['jsList'],$classes);
      }
    

    输出 classes 发现排序已经实现,但是始终无法用排序后的数据替换 areas 中的 jsList,想知道错在了哪里。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1085 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:26 · PVG 07:26 · LAX 15:26 · JFK 18:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.