dongni9825 2015-08-15 15:06
浏览 174

如何在PHP中将八进制值转换为字符串?

One of my webs service is responding value in octal(010), I want to use this value as key in an array.

$key = 010;
$a[$key] = 'test';
print_r($a);

Result :

Array
(
    [8] => test
)

Expected :

Array
(
    [010] => test
)

PHP is converting 010 value to 9 even I do typecasting, I tried (string) $key and sprintf('%s', $key) but no luck.

Is there anyway do we have to convert octal to string without change the value?

  • 写回答

2条回答 默认 最新

  • douqiang4501 2015-08-15 15:08
    关注
    $key = sprintf('%o', $key);
    

    see http://docs.php.net/manual/en/function.sprintf.php
    (but then your key is a string, not an integer anymore)

    edit: not sure if you want the leading 0 to be printed always

    $key = sprintf('0%o', $key);
    

    or to always get a key with three digits

    $key = sprintf('%03o', $key);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器