Set value of an array using xpath notation
This is a continuation to my previous article on how to get values of multi-dimensional arrays using xpath notation.
This time I will show you how to set a value of an array using the same xpath notation (root/branch/leaf).
Get values of multi-dimensional arrays using xpath notation
Sometimes we have a value burried deep within a multi-dimensional array. In order to access it we usually end up doing something like this:
- $value = $array['a']['b']['c'];
hoping that all the keys are set. However, we have a problem if 'b' is not set or any other of the "path" keys are missing.
Wouldn't it be nice if we could get the value using something similar to xpath, e.g. "a/b/c" and specify the default value if the chain is broken? I thought it would and wrote this little function or/and extension to ArrayObject.
Get the last effective URL from a series of redirects for the given URL
As some of you may know websites like Facebook or Twitter allow you to fetch user profile images if you know id of the given users. For example, you can fetch the Facebook image for Mark Zuckerberg by going to http://graph.facebook.com/4/picture (4 is Mark's user id), however if you pay attention to the address bar you will notice that the the aforementioned URL actually redirects to http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg.
Same goes for Twitter - I could fetch my user picture by going to http://api.twitter.com/1/users/profile_image/codeaid but it actually redirects to https://si0.twimg.com/profile_images/321201130/twitter_bigger_normal.png.
In one of the projects I've been working on lately, I added a functionality for people to log in using their Facebook, Twitter and other service credentials. I am also storing URLs of their user images in the database for displaying on the comment pages. As you saw earlier it is easy to generate URLs redirecting to the correct destination, however as you can imagine it doesn't make sense to store them as they are as you would have to execute those redirects every time you access the image. Imagine if you have a page with comments and every comment has a picture - it could and would take a long time to resolve and load them all.
That lead me to writing a simple function, which takes the starting URL, follows all the redirects and returns the last effective URL for it. From examples above the last effective URL for http://graph.facebook.com/4/picture would be http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg.
Improved var_dump() with colored output
As developers, we all do some debugging within the code that we are writing at that moment. Usually the first choice is either calling var_dump or print_r depending on what we are used to but it usually ends the same way:
- print '<pre>';
- var_dump($something);
- // or print_r($something);
Usually the <pre> tag is not even included due to us being lazy and we end up viewing the source because the output that we got in Times New Roman is not really that readable.
Of course, there is Xdebug which we can used but not all of us have control over our webservers or desire to install/enable any extensions.
Here is a class, which you will be able to use in all those occasions.
It could as well be considered an improvement over the class mentioned in one of my previous articles however this class does not change the format of the output, only its colors.
Generate a string of random characters
Here's a quick function for all those occasions when you need to generate a string of random characters in javascript.


