Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-26-2009, 03:28 AM   PM User | #1
Hayyel
New Coder

 
Join Date: Jan 2009
Posts: 91
Thanks: 17
Thanked 1 Time in 1 Post
Hayyel is an unknown quantity at this point
Change key name in 2-Dim Array

Hello,

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:

PHP Code:
Array
(
    [
1] => Array
        (
            [
username] => Anonymous
        
)

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

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

How do I change the [username] to [name]?
Hayyel is offline   Reply With Quote
Old 03-26-2009, 03:56 AM   PM User | #2
steelaz
New Coder

 
Join Date: Mar 2009
Location: Chicago, IL
Posts: 69
Thanks: 0
Thanked 15 Times in 15 Posts
steelaz is an unknown quantity at this point
If you just want to change index name, try this:

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


Last edited by steelaz; 03-26-2009 at 04:07 AM..
steelaz is offline   Reply With Quote
Old 03-26-2009, 05:01 AM   PM User | #3
Hayyel
New Coder

 
Join Date: Jan 2009
Posts: 91
Thanks: 17
Thanked 1 Time in 1 Post
Hayyel is an unknown quantity at this point
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?
Hayyel is offline   Reply With Quote
Old 03-26-2009, 01:09 PM   PM User | #4
steelaz
New Coder

 
Join Date: Mar 2009
Location: Chicago, IL
Posts: 69
Thanks: 0
Thanked 15 Times in 15 Posts
steelaz is an unknown quantity at this point
Yes, also, take a look at examples using array_multisort().
steelaz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:36 AM.


Advertisement
Log in to turn off these ads.