View Full Version : Change key name in 2-Dim Array

03-26-2009, 03:28 AM

I have recieved excellent help here before and after many hours of no results on my own thought I would come back and ask.

If I have:

[1] => Array
[username] => Anonymous

[2] => Array
[username] => Test

[3] => Array
[username] => AdsBot [Google]

How do I change the [username] to [name]?

03-26-2009, 03:56 AM
If you just want to change index name, try this:

$new_array = array();
foreach ($original_array as $key => $value)
$new_array[$key] = array('name' => $value['username']);

03-26-2009, 05:01 AM
That worked great. Now that the two arrays are the same as far as format I thought I could run an array_diff on them but it returns as an empty array. I am assuming this is because they are two dimensional arrays.

I'll have to sort them somehow and then compare them... probably with some sort of nested foreach loop.

Am I on the right track?

03-26-2009, 01:09 PM
Yes, also, take a look at examples using array_multisort() (http://www.php.net/manual/en/function.array-multisort.php).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum