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

2019-04-05 13:13:15 +08:00
 Test2g

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

原始 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,想知道错在了哪里。

788 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/552310

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX