Here is another PHP thing I need to think clearly about from time to time:
var_dump( [ 'a' => 1 ] + [ 'a' => 2, 'b' => 2 ] );
Returns:
array(2) { 'a' => int(1) 'b' => int(2) }
Here is another PHP thing I need to think clearly about from time to time:
var_dump( [ 'a' => 1 ] + [ 'a' => 2, 'b' => 2 ] );
Returns:
array(2) { 'a' => int(1) 'b' => int(2) }
Some notes about arrays in PHP over here: PHP: Frankenstein arrays. I was already aware of most of that but I thought the notes at the bottom about supporting JSON were handy:
If you want to enforce an array to encode to a JSON list (all array keys will be discarded), use:
json_encode(array_values($array));
And if you want to enforce an array to encode to a JSON object, use:
json_encode((object)$array);
Also array_is_list is available as of PHP 8.1.
Today I stumbled upon SliceTricks (over here). It’s about how to do array manipulations with slice syntax in Go.
Everything you ever wanted to know about sorting arrays in PHP.
Found myself looking up the syntax of the Array splice method.
Apparently in PHP 5.4 you can index an array returned from a function as explained here. I learned this today but unfortunately I’m still running PHP 5.3. I also read an article on stackoverflow about how to make an object behave like an array.
The file function parses a text file into an array, and the file_get_contents function returns a text file as a string.
I just learned about the difference between delete and delete[] in C++ by reading this article from StackOverflow. Basically you use delete[] to delete arrays, and delete for everything else. There was conflicting information about whether C++ runs the destructors on objects in an array when the array is deleted. Some people said it did, others said it didn’t. I should do an experiment to see one day. The reason for the difference between delete and delete[] seems to be that when C++ allocates an array it allocates memory for storing the size of the array as well as the array elements, and then returns a pointer to the first array element, which is beyond the start of the allocated memory, because the array size takes up the first bit of space.