制作关联数组

Wil*_*onG 0 php arrays string integer associative-array

我的目标是从for循环的值中创建ant assoc数组.

//$from_time value is 6 and $to_time value is 23

for ($i = $from_time; $i <= $to_time; $i++) {
        $working_time_array[] = $i;
    }
echo json_encode($working_time_array);
Run Code Online (Sandbox Code Playgroud)

我获得了AJAX成功的输出,当我在console.log中时,我得到了结果:

["6",7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Run Code Online (Sandbox Code Playgroud)

优选的结果是

["6","7","8","9","10"]... etc
Run Code Online (Sandbox Code Playgroud)

Joh*_*nde 5

两个结果之间的唯一区别是一个结果集包含整数而另一个包含字符串.如果您希望这些值为字符串,则只需在将它们分配给数组时进行转换:

for ($i = $from_time; $i <= $to_time; $i++) {
    $working_time_array[] = (string) $i;
}
Run Code Online (Sandbox Code Playgroud)

除非您的客户端只期待字符串,否则这确实不应该是必要的.