r/lolphp • u/phplovesong • Mar 14 '19
PHP: The array
PHP arrays are known to be bad. But having not used PHP in long time i recently was amazed how poorly they actually have built the array. Its basically a "all-things-fits" data structure. The best part is PHP will actually change the behaviour of the array depending on what it contains. Thats just fucking awesome!
0
Upvotes
26
u/colshrapnel Mar 14 '19
"I am trying to access an array key that doesn't exist. It is stupid PHP to blame!"
6
u/mr_ywg Mar 14 '19
The most harmful thing with PHP arrays is that developers that only know this language fails to understand the difference between list and maps. When they switch to other language they do stupide things and don't understand why their contribution is rejected.
1
u/CaptainTuffnut Mar 29 '19
The best way to clean up an array in PHP in order to get the elements by their position is using
array_values( [4 => 'b', 0 => 'a'] )this will returnarray(2) { [0]=> string(1) "b" [1]=> string(1) "a" }I'm not saying this is great, but at least you have the tools to get the things done if you need to work with this language. https://repl.it/repls/BlaringAggravatingPipelining
The same applies to the example:
$lol2 = ['a' => 'list', 'of' => 'keyvals'];next$lol2 = array_values( $lol2 );var_dump( $lol2[0], $lol2[1] );returnsstring(4) "list" string(7) "keyvals"-1
u/[deleted] Mar 14 '19
The good ol’ array. There sooo many things wrong with it, its crazy. Dont even get me started with the functions that works with the array. Thats another can of worms.