Reputation: 1694
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?
Upvotes: 3
Views: 1573
Reputation: 198219
Is there anyway do we have to convert octal to string without change the value?
Yes, as outlined, e.g. see the answer by VolkerK or by Anonymous.
However there is perhaps a little thing lost in translation, let's see expected vs. actual again:
Result :
Array
(
[8] => test
)
Expected :
Array
(
[010] => test
)
When a PHP array has an integer key, print_r
will always show its value in decimal - never octal.
Using an octal integer for the key therefore displays it decimal.
Using a string key then can work, however it is a requirement to prefix it with 0
as otherwise PHP would automatically convert it to an integer:
$key = 010;
$array = array(sprintf('0%o', $key) => 'test');
print_r($array);
As the leading zero preserves the string-key, print_r()
now displays it as if if would be an octal number:
Array
(
[010] => test
)
Note thought, that then it is required to use the string '010'
to access this array member as well.
The behavior of PHP array integer vs. string keys is outlined in the PHP manual on the Arrays page. Specifically this part may be interesting:
The key can either be an int or a string. The value can be of any type.
Additionally the following key casts will occur:
- Strings containing valid decimal ints, unless the number is preceded by a + sign, will be cast to the int 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.
- Floats are also cast to ints, which means [...]
When in doubt also use a function to inspect the array that shows the type of the key, for example var_export()
or var_dump()
.
array(1) { array (
["010"]=> '010' => 'test',
string(4) "test" )
}
Example var_dump()
output (left) and var_export()
output (right).
Upvotes: 0
Reputation: 96189
$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);
Upvotes: 3
Reputation: 12100
In PHP (and essentially every other language), these types of declarations are allowed, but are always converted to a decimal integer (binary internally) at compile/parse time. So when the script is running, $key
will always be 8
. If you want to use the octal value as a key, you need a function that formats numbers into other bases (which will be represented as a string).
$num = 8;
$key = $num === 0 ? '0' : '0' . decoct(8); // works for all numbers 0 and up
$a[$key] = 'test';
print_r($a);
Upvotes: 1