php sort / asort无法按预期工作

时间:2017-04-27 13:02:29

标签: php arrays sorting

我遇到了排序PHP数组的问题。我在MYSQL表中有一些信息,其中包含一列PO#。根据与其相关联的工作类型,有两种类型的PO#。一个PO是一系列以17为前缀的数字,不同类型的作品有一系列以NC为前缀的数字。创建记录时,我需要确定工作类型然后搜索与该类型工作相关联的PO,找到该PO系列中的最大值并添加1.目前,我正在尝试拉取PHP数组根据工作类型,对数组进行排序,然后返回数组中的end()值以添加一个。问题在于排序不像我期望的那样有效。

这是我目前正在使用的PHP,以查看数组是否正确排序:

$worktype = mysqli_real_escape_string($db, $_POST['worktype']);
  $poArr = [];

  if ($worktype == 1) {
    $getPO = "SELECT po FROM jobbooktbl WHERE substr(po, 1,2)!='NC'";
    $qrygetPO = mysqli_query($db,$getPO);
    while ($row = mysqli_fetch_array($qrygetPO)) {
      array_push($poArr, $row['po']);
    }
  } else if ($worktype == 2) {
    $getPO = "SELECT po FROM jobbooktbl WHERE substr(po, 1,2)='NC'";
    $qrygetPO = mysqli_query($db,$getPO);
    while ($row = mysqli_fetch_array($qrygetPO)) {
        $po = substr($row['po'], 3);
        array_push($poArr, $po);
    }
  }

  $poArr[] = asort($poArr);

  print_r($poArr);

这是打印的阵列:

Array ( [235] => [237] => 1 [236] => 1 [238] => 1 [239] => 1 [0] => 17001 [1] => 17002 [2] => 17003 [3] => 17004 [4] => 17005 [5] => 17006 [6] => 17007 [216] => 17008 [217] => 17009 [218] => 17010 [219] => 17011 [220] => 17012 [221] => 17013 [222] => 17014 [223] => 17015 [7] => 17016 [8] => 17017 [224] => 17018 [225] => 17019 [226] => 17020 [9] => 17021 [10] => 17022 [11] => 17023 [227] => 17024 [228] => 17025 [12] => 17026 [229] => 17027 [230] => 17028 [13] => 17029 [14] => 17030 [15] => 17031 [16] => 17032 [17] => 17033 [18] => 17034 [19] => 17035 [20] => 17036 [21] => 17037 [22] => 17038 [23] => 17039 [231] => 17040 [24] => 17041 [25] => 17042 [26] => 17043 [27] => 17044 [28] => 17045 [29] => 17046 [30] => 17047 [31] => 17048 [32] => 17049 [33] => 17050 [34] => 17051 [35] => 17052 [36] => 17053 [37] => 17054 [38] => 17055 [39] => 17056 [40] => 17057 [41] => 17058 [42] => 17059 [43] => 17060 [44] => 17061 [45] => 17062 [46] => 17063 [47] => 17064 [48] => 17065 [49] => 17066 [50] => 17067 [51] => 17068 [52] => 17069 [53] => 17070 [54] => 17071 [55] => 17072 [56] => 17073 [57] => 17074 [58] => 17075 [59] => 17076 [60] => 17077 [61] => 17078 [62] => 17079 [63] => 17080 [64] => 17081 [65] => 17082 [66] => 17083 [67] => 17084 [68] => 17085 [69] => 17086 [70] => 17087 [71] => 17088 [72] => 17089 [73] => 17090 [74] => 17091 [75] => 17092 [76] => 17093 [77] => 17094 [78] => 17095 [79] => 17096 [80] => 17097 [81] => 17098 [82] => 17099 [83] => 17100 [84] => 17101 [85] => 17102 [86] => 17103 [87] => 17104 [88] => 17105 [89] => 17106 [90] => 17107 [91] => 17108 [92] => 17109 [93] => 17110 [94] => 17111 [95] => 17112 [96] => 17113 [97] => 17114 [98] => 17115 [99] => 17116 [100] => 17117 [101] => 17118 [102] => 17119 [103] => 17120 [104] => 17121 [105] => 17122 [106] => 17123 [107] => 17124 [108] => 17125 [109] => 17126 [110] => 17127 [111] => 17128 [112] => 17129 [113] => 17130 [114] => 17131 [115] => 17132 [116] => 17133 [117] => 17134 [118] => 17135 [119] => 17136 [120] => 17137 [121] => 17138 [122] => 17139 [123] => 17140 [124] => 17141 [125] => 17142 [126] => 17143 [127] => 17144 [128] => 17145 [129] => 17146 [130] => 17147 [131] => 17148 [132] => 17149 [133] => 17150 [134] => 17151 [135] => 17152 [136] => 17153 [137] => 17154 [138] => 17155 [139] => 17156 [140] => 17157 [141] => 17158 [142] => 17159 [143] => 17160 [144] => 17161 [145] => 17162 [146] => 17163 [147] => 17164 [148] => 17165 [149] => 17166 [150] => 17167 [151] => 17168 [152] => 17169 [153] => 17170 [154] => 17171 [155] => 17172 [156] => 17173 [157] => 17174 [158] => 17175 [159] => 17176 [160] => 17177 [161] => 17178 [162] => 17179 [163] => 17180 [164] => 17181 [165] => 17182 [166] => 17183 [167] => 17184 [168] => 17185 [169] => 17186 [170] => 17187 [171] => 17188 [172] => 17189 [173] => 17190 [174] => 17191 [175] => 17192 [176] => 17194 [177] => 17195 [178] => 17196 [179] => 17197 [180] => 17198 [181] => 17199 [182] => 17200 [183] => 17201 [184] => 17202 [185] => 17203 [186] => 17204 [187] => 17205 [188] => 17206 [189] => 17207 [190] => 17208 [191] => 17209 [192] => 17210 [193] => 17211 [194] => 17212 [195] => 17213 [196] => 17214 [197] => 17215 [198] => 17216 [199] => 17217 [200] => 17218 [201] => 17219 [202] => 17220 [203] => 17221 [204] => 17222 [205] => 17223 [206] => 17224 [207] => 17225 [208] => 17226 [209] => 17227 [210] => 17228 [211] => 17229 [212] => 17230 [213] => 17231 [214] => 17232 [215] => 17233 [232] => 17234 [233] => 17235 [234] => 17236 [240] => 1 )

正如你所看到的那样,阵列似乎已经完全排序,直到最后一个项目由于某种原因是1。我不明白为什么会发生这种情况。

0 个答案:

没有答案