If you’re brand new to CakePHP web development framework, you may be asking yourself “How do I get rid of the cake-sql-log footer on my pages?” The answer is that you are still in debug/dev mode, and can get rid of the footer by changing debug to 0 in your config/core.php file.
Tag Archives: php
Remove PHP Warning “Unable to load dynamic library” mhash after upgrading Ubuntu
After upgrading Ubuntu to 10.04 (give or take a version), you might notice the following warning (or variant) thrown by PHP5:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626/mhash.so' - /usr/lib/php5/20090626/mhash.so: cannot open shared object file: No such file or directory in Unknown on line 0
This is the result of the latest version of PHP already having mhash included in the php5-common package. Even though php5-mhash got removed in the upgrade, the mhash.ini file still references it (and the warning gets thrown).
To fix this, you’ll need to comment out the following line in /etc/php5/cli/conf.d/mhash.ini :
extension=mhash.so
This is a hack, but it removes the tiresome warning.
Multidimensional Array Sorting in PHP (sorting arrays of arrays)
For today’s PHP coding trick, the function usort is your friend, though I found the PHP documentation wanting on this topic.
Basically, you have an array of arrays (multidimensional) and you want to sort the first array by the values in the secondary arrays. In my case, I had an array of 3 value associative arrays (title, link, date). I wanted to sort the primary array by the datetimes found in the 2nd array. Turns out, this is incredibly easy using usort.
Just create a comparative function that will take two children arrays and compare two elements. In my case, comparing articles (or more specifically, the dates):
function compare_times($article_one, $article_two) {
if ( $article_one['Date'] == $article_two['Date'] )
return 0;
else if ( $article_one['Date'] < $article_two['Date'] )
return 1;
else
return -1;
}
Then… just call that by giving usort two parameters: the master array and the compare function name as a string:
usort($todays_stories[$section], 'compare_times');
(N.B. In my case above, I was using a three dimensional array, but only sorting the 2nd dimension ($section) by the times (more specifically the ‘Date’ key) in the third dimension. The first dimension went unsorted/untouched. This was inside of a foreach loop, so each $section was sorted independently from most recent article to oldest article).
Hope this helps — comment below if you have questions.