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条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址