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);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能