Last active
August 29, 2015 14:05
-
-
Save ijansch/862ef3bee20f94f73815 to your computer and use it in GitHub Desktop.
2 common issues in php based apis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Issue 1, empty associative arrays (dictionaries) become regular arrays. | |
$dictionary = array('key' => 'value'); | |
var_dump(json_encode($dictionary)); // gives '{"key":"value"}'; | |
unset($dictionary['key']); | |
var_dump(json_encode($dictionary)); // Gives '[]' -> json dictionary turned into array | |
var_dump(json_encode((Object)$dictionary)); // Gives '{}' -> json dictionary stays dictionary | |
// Issue 2, array manipulations that involve the array indexes, turn numeric arrays into dictionaries. | |
$fruits = array("apples", "oranges"); | |
var_dump(json_encode($fruits)); // Gives ["apples", "oranges"] | |
unset($fruits[0]); // Don't like apples. | |
var_dump(json_encode($fruits)); // Gives '{"1":"oranges"}' -> wow, array turned into dictionary. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i had this problem dealing with mongoDB records. I pulled out most of my hair trying to debug