php array behaving strangely with key value 07 & 08
Prepending 0 before a number means PHP parses it as an octal value in the same way that prepending 0x causes it to be parsed as a hexadecimal value. Remove the zero, and it will work fine.
echo 07; // prints 7
echo 010; // prints 8
This is mostly used when specifying unix permissions:
chmod("myfile", 0660);
Except for that it's rarely something that you'd want to do.
This is described in the PHP Manual.
php array bug in index 000001
Numbers prefixed with 0
are treated as octal. Octal 17 -> decimal 15.
echo 01 -> 1
echo 02 -> 2
...
echo 07 -> 7
echo 08 -> 0 (failure)
echo 09 -> 0 (failure)
echo 019 -> 1
which means $array[019]
is effectively identical to $array[1]
, because 019
-> 1
, as it's an invalid octal number, and the 9
is stripped.
Weird behavior of matching array keys after json_decode()
It can be further simplified to this problem
$o = new stdClass;
$o->{5} = 1; //or even $o->{'5'} = 1;
$a = (array) $o;
print_r($a); // Array([5] => 1)
var_dump(isset($a[5])); // false
var_dump(isset($a['5'])); // false
It seems that this only happens when the property name is one that php would normally consider a numeric key in an array.
Definitely unexpected behavior to me.
edit
Same issue here
Casting an Array with Numeric Keys as an Object
edit#2
documented behavior
http://www.php.net/manual/en/language.types.array.php#language.types.array.casting
Exactly matching the keys of a PHP array
You might not know it but you can simply use ==
for array comparison
if($array1==$array2)
{
//same key value pairs
}
Fiddle
var_dump($searcharray==$resultsarray1);
var_dump($searcharray==$resultsarray2);
var_dump($searcharray==$resultsarray3);
var_dump($searcharray==$resultsarray4);
var_dump($searcharray==$resultsarray5);
var_dump($searcharray==$resultsarray6);
Output
bool(false)
bool(false)
bool(false)
bool(true) //4
bool(true) //5
bool(false)
And interestingly
If you want to be even more strict and require that they should return true even if the order of keys is same then you can simply use ===
Fiddle
Array element being seemingly ignored?
I found out what was causing the problem. I was exporting the excel spreadsheet as a UTF8 encoded CSV. If I export it as a plain CSV, the key doesn't include any unprintable characters
PHP: Strange Array Behaviour After Object Type Casting to Array
This seems to be related to bug #61655 fixed in 7.2.0:
in a object property lookup by name always in string, but in array numeric
string(like "22200" ) key will transform to numeric but not a string anymore.
when conversion internal HashTable did't changed so after conversion, key
lookup will fail.
Clarified: $a["2000"]
is always interpreted as $a[2000]
, but (array)
failed to cast object string keys to numbers. So the array contained string numeric indices, but the array syntax' automatic casting prevented those from being accessible.
PHP can't get output from array
There are two issues here:
- Array indices behave differently between strings and numbers,
- Variable scope of
$minarray
.
$arr[01]
and $arr['01']
are not the same thing, so you should be more explicit; in your case you can just leave the array numerically indexed, i.e.:
$minarray = array('00', '02', '03', '05', ...);
Then, you use an (int)
cast on the given minutes:
$finishmins = $minarray[(int)$finishmins];
You can solve the second issue by passing the array as a function argument:
function finishtime($minarray, $finish)
Then calling it like so:
echo finishtime($minarray, '12:01');
How to push both value and key into PHP array
Nope, there is no array_push()
equivalent for associative arrays because there is no way determine the next key.
You'll have to use
$arrayname[indexname] = $value;
Related Topics
PHP Passing C Struct Data Throught Socket Programming
Send Cookie with File_Get_Contents
Bcrypt and Randomly Generated Salts
How to Find Unused Functions in a PHP Project
.Htaccess Issues: No Input File Specified
Storing Form Data as a Session Variable
Special Characters in PHP/Mysql
Why Does the Laravel API Return a 419 Status Code on Post and Put Methods
Remove .PHP Extension with PHP
Check Whether a Request Is Get or Post
Why We Need Curlopt_Ssl_Verifypeer in Windows
Simple HTML Dom: How to Remove Elements
Binding Parameters for Where in Clause with Pdo
Getting Http Code in PHP Using Curl