Sort multidimensional array in php

Filed under: Web Development — rasim

Haven’t wrote anything here for a while, been really busy lately plus I just got a house, so I have way more work than I can handle. I’ve been reading a bit on SEO and started to put some things into action by starting to build my mini network. While building a few new sites, I started to create content using separate databases and php multidimensional arrays worked great for what I needed. The only problem is sorting multidimensional array as needed. :)

So at the end I came up with an array like this:


$profile[0]['user'] = 'a user';
$profile[0]['name'] = 'user a name';
$profile[1]['user'] = 'c user';
$profile[1]['name'] = 'user c name';
$profile[2]['user'] = 'c user';
$profile[2]['name'] = 'c name';

etc…

Here is the function that sorts this array:

//sort multidimensional array
function sort_multi($arr, $arr_key) {
  //loop through array
  foreach($arr as $key_1=>$val_1) {
    //new array with the $arr_key element's value that needs to be sorted
    $temp_arr[$key_1] = $val_1[$arr_key];
  }
  //Sort array while maintaining index association
  asort($temp_arr);
  //put back into multidimensional array
  foreach($temp_arr as $key_2=>$val_2) {
    $final_arr[] = $arr[$key_2];
  }
  return $final_arr;
}

Now I can sort by user:


$sorted_by_user = sort_multi($profile, "user");

or by name:


$sorted_by_name = sort_multi($profile, "name");

You can use arsort() inside the function to sort the array in reverse order if needed.

Generally I avoid using more complex operations with multidimensional arrays and do it on the database level if possible as it is easier and can be more efficient especially if there are millions of records.

Pagerank update should be coming up

Filed under: SEO & SEM — rasim

Update: Pagerank update happened just a few hours after I finished writing this post on April 03, 2010. This blog shows pr2 now with some pr1-pr2 pages. The next one should be in the first days of July…

Last pagerank update happened in December 31, 2009 – January 1, 2010, which means there should be another pagerank update this month, hopefully by mid-April. For those who believe that pagerank is irrelevant, here is a good recent article – Matt Cutts’ Comments on Crawling & Indexation. It certainly doesn’t mean as much as it used to, but it is still a big part of the algorithm. :)

I haven’t posted for a while, mostly because I’ve been pretty busy with a few new projects and SEO experiments. It is pretty cool what a keyword rich domain name, a dedicated server plus a few relevant links can do for ranking. I was even able to hit #1 position for a simple 1 page landing page.

I am thinking about getting my own colocated server soon and close four hosting accounts that I have now. It will be just a little more than what I pay right now for all four, I’ll probably end up picking something like quad xeon with 8-16gb of ram from ebay. It should be waaay faster than all servers that I have now put together. I believe fast and reliable server will help the rankings even more.

EWA Network