duanla3319 2015-02-18 18:32
浏览 99
已采纳

不希望重新排序PHP数组中的键

I have a quick question. When building an associative array key casting rules mean that strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. (On the other hand "08" will not be cast, as it isn't a valid decimal integer.) See for example: http://php.net/manual/en/language.types.array.php

The problem I have is that my keys are mixed integer and string .. meaning that when the associative array is built, all keys are reordered with numerical keys appear first before string. This is a sample of what I get in my console log:

...
2032: "9371.84"
2033: "9351.60"
2034: "9331.36"
2035: "9311.12"
ID: "1"
Misc1: "Russian Federation - Conventional"
Misc2: "RUS.Con1"
Misc3: "4"
Misc4: ""
...  etc.

How can I avoid this issue, so that the associative array does not re-order my keys?

As an FYI, this is how I generate my array in PHP:

while ($array = mysqli_fetch_assoc($result)) {
$experiment[] = $array;
};

Thank you for your time, G.

  • 写回答

2条回答 默认 最新

  • dousi6303 2015-02-19 16:13
    关注

    The workaround for the issue I have found is to avoid the associative array structure. This is what my loop looks like now:

    while ($array = mysqli_fetch_assoc($result)) {
    $experiment[0] = array_keys($array);
    $experiment[] = array_values($array);
    };
    

    The annoying thing is that $experiment[0] = array_keys($array); gets looped uneccessarily... but at least I get the result that I am looking for and the keys are not cast and re-ordered by the associative array.

    If anybody knows how to avoid the unecessary looping for $experiment[0], then please let me know :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch