Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post

    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]?

  • #2
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    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 05:07 AM.

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post
    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?

  • #4
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Yes, also, take a look at examples using array_multisort().


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •