How To Merge Object Variables in PHP
$merged_object = (object) array_merge((array) $object1, (array) $object2);
$merged_object = (object) array_merge((array) $object1, (array) $object2);
Use Case: Export Apple Reminders from MacOS application to cleaned plain text, i.e. remove unwanted spaces, blank lines and square brackets PROCEDURE Click on a single item in the MacOS Reminders app list Select All (either Command-a or use Edit->Select All) Copy (either Command-c or use Edit->Copy) Open Terminal Run cleanme Paste the copied reminders […]
Here is a one-liner that explodes a string and trims whitespace from each element: $array = array_map(‘trim’,explode(‘,’,$str));
Coming from the Perl world, I wanted to emulate the following code in JavaScript:
1 2 3 4 5 6 7 |
my $myHash = { 'foo' => 'Hello', 'bar' => 'World', }; foreach my $myKey (keys %$myHash) { print $myKey,'=',$myHash->{$myKey}; } |
What I found is that JavaScript uses “Objects” to contain hash-style data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Define the hash object on the fly var myHash = { foo: "Hello", bar: "World" }; // loop through all keys for (var myKey in myHash) { alert(myKey + '=' + myHash[myKey]); } // access a key individually alert('foo=' + Hash.foo); alert('bar=' + Hash.bar); |
Also, one may use variables as key names by using the square-bracket notation, which evaluates variables first:
1 2 3 4 |
var myVar = 'test'; //These two lines are equivalent as long as myVar is 'test': myHash[myVar] = 'theValue'; myHash.test = 'theValue'; |
There are 4 “key” functions for dealing with PHP array sorting. These four have the significant feature of keeping the associated key/value relationships intact. asort() – sort by value ascending arsort()– sort by value descending ksort()– sort by key ascending krsort() – sort by key descending For more information check out this php man page […]