Sorting a Multi-Dimensional Array with PHP


//an array of some songs I like
$songs =  array(
		'1' => array('artist'=>'The Smashing Pumpkins', 'songname'=>'Soma'),
		'2' => array('artist'=>'The Decemberists', 'songname'=>'The Island'),
		'3' => array('artist'=>'Fleetwood Mac', 'songname' =>'Second-hand News')
	);

The problem is thus: I’d like to echo out the songs I like in the format “Songname (Artist),” and I’d like to do it alphabetically by artist. PHP provides many functions for sorting arrays, but none will work here. ksort() will allow me to sort by key, but the keys in the $songs array are irrelevant. asort() allows me to sort and preserves keys, but it will sort $songs by the value of each element, which is also useless, since the value of each is “array()”. usort() is another possible candidate and can do multi-dimensional sorting, but it involves building a callback function and is often pretty long-winded. Even the examples in the PHP docs references specific keys.
Continue reading

Advertisements

How can we get the properties (size, type, width, height) of an image using PHP image functions?


To know the Image type use exif_imagetype () function
To know the Image size use getimagesize () function
To know the image width use imagesx () function
To know the image height use imagesy() function

What is PHP’s mysqli Extension?


The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. The mysqli extension is included with PHP versions 5 and later.

The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:
=>Object-oriented interface
=>Support for Prepared Statements
=>Support for Multiple Statements
=>Support for Transactions
=>Enhanced debugging capabilities
=>Embedded server support

How to show page load time in PHP ?


If you need to calculate how much time elapsed during this the queries execute on the page.

The answer is:

$start = microtime(true);
$end = microtime(true);
printf("Page was generated in %f seconds", $end - $start);

For reference, microtime() in a PHP predefined function.

Write $start just before your query or the beginning of the page and $end at the end of all the queries or at the end of page.

Allow PHP in WordPress Widgets without any plugin


Sometimes in your WordPress theme you need to execute custom php code in a widget, because you want to display different information according to the category you are in, or simply because you need to execute a php script into that widget.

There are a lot of plugins doing this task, adding a new type of widget generally called “php widget”, but rather than installing a plugin this simple job can be done simply adding in functions.php file of your theme these few lines:

add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}

Now, when you add a text widget to your site, you can use PHP.

Generate random password in PHP


When creating web apps, there’s often a need to generate a random password for your users. It uses PHP’s handy str_shuffle() function:


function random_password( $length = 8 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
    $password = substr( str_shuffle( $chars ), 0, $length );
    return $password;
}

$password = random_password(8);

PHP mail() header


PHP mail() header:


$headers  = "From: your-name  \r\n";
$headers .= "Reply-To: your-name  \r\n";
$headers .= "Return-Path: your-email \r\n";
$headers .= "Bcc: first-mail , second-mail \r\n";
$headers .= "PHP/" . phpversion();

$to = "receivers-email";
$message= "your-message";
$subject = "email-subject";

mail($mailto, $subject, $message, $headers);