How to replace keys in an associative array

PHP has a pretty decent set of array functions for manipulating arrays, but I was a little surprised to see few functions that dealt with key manipulation.

It might not be quite as common a need to manipulate or replace the keys of an associative array, but it could be quite useful when maintaining a standardised format, for example when dealing with user data. Another possible use could be when displaying data directly from a database table.

It’s often convenient to use the database table column names when displaying table data, but those column names might not necessarily be in a user-friendly format. It’s fairly common to see underscores or CamelCase used in column names for readability. Great for programmers, but not so great for published content.

When working with query results as an associative array, this function could be one way to deal with that issue. For underscores, a simple find/replace. For CamelCase, a space before the second capitalised letter. Depending on your circumstances, this might be better accomplished with SQL aliases for simpler column name replacements.

Replacing a key is a little trickier than replacing a value, because you can’t change the key name directly. Instead, you copy the value from the old key, assign it to the new key, then delete the old key where the value was originally held.



This function is relatively straight forward. It checks to see if a key exists in a 2-dimensional array, and replaces the key with function parameter $newkey. The function returns true on success, and false on failure.

Example usage


This example demonstrates directly replacing one key with another, and using a foreach loop to iterate over all of the keys and make them upper case. This second example actually closely resembles the built-in PHP function array_change_key_case(), however by iterating over the keys with replaceKey(), any sort of string manipulation may be performed on the keys.

Logging: A quick and simple way to log output in PHP

If you’ve ever used JavaScript or jQuery, then you’ve probably been spoiled by the built-in console that comes with most modern web browsers. A quick call to console.log() is all it usually takes to quickly inspect your data, providing a fast and invaluable debugging tool while you’re developing your project.

Unfortunately, being a server-side language, PHP isn’t afforded the same luxury, and although plenty of solutions do exist to solve this problem, most of them are often overkill when all you want to do is peek at an array or the contents of a variable during code execution. Debuggers and Unit Testers are available in many IDEs, which are great for complex object-oriented code and when you need to do more than just peek at a value, but more often than not, I find myself using this;

It’s probably one of my most-used code-snippets, because the majority of the time I just want to ensure the data I’m returning from a database query is correct, or that an array I’ve dynamically-built contains the correct data in the right structure. It means I can make alterations quickly, when stepping through a debugger or setting up Unit Tests would take longer. The pre tags ensure the browser preserves spacing and line-breaks, and print_r() provides a nice clean readable format for arrays;

Although this method can be quick, it can also be inconvenient to stop the entire execution of your project. Sometimes however this is unavoidable, especially when working with MVC frameworks, as the final page your browser displays might not be the one you’ve printed your output to. You may wish to see output from your controller or your model, but without stopping execution flow, your application may render your view and prevent you from seeing your test output. Then there’s production code. If you have no other options than to work directly on a live system, then die() is one of the last functions you’d want to use.


A more elegant solution would be to create a helper class that would allow you to log data at any stage during your application.

Then call the static method, passing in your array or variable;

The method will append to the log by default, unless you pass in a different mode. It will generate a html log file in the source directory of the class. You can of course change this if you wish to output your log within a particular directory. Reloading the log is then just a simple matter of hitting refresh in your browser.


While these quick code snippets are by no means a solution to every debugging problem you’ll face, they’re still a useful and time-saving strategy when you need quick access to variable output.