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.





At first I doubted it worked, but after breaking it down I see that you effectively sorted the indexes, nicely done.
Good stuff rasim! i like it