SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: php array

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php array

    I have a function which returns an array like this:

    myarray[0]['name'] = value 1
    myarray[1]['name'] = value 2
    myarray[2]['name'] = value 3
    etc..

    obviously adding ['name'] on the end is redundant, so how can i convert the above array to simply

    myarray[0] = value1
    myarray[1] = value2
    etc...

    i could do it by creating a temp variable and manually moving values using a for loop, but surely there's a better way?

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just alter the function to return the correct array?

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, more specifically, it's returned from a mysql query, so, ye, i want the function to return the array properly...as i say, i know of a way of doing it, but surely there's a php function to do this nicely?

    maybe a better way to ask the question is, when extracting rows of data from a mysql query for just one field, can you make it return the array as simply $result[index] instead of $result[index]['fieldname'] ?

  4. #4
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know what to do I add for array, but u don't need it
    I have no Idea

    sorrry for that
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.

  5. #5
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can simply loop through the original array and then create a new array as you need it. That would be the best way I can think of right now.

    Edit :
    Just as "ruba" showed you up there. That should work. Are there any limitations?

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ye..what ruba works is pretty much what i had done, i just assumed there'd be some php function to remove "redundant" array indexes or another way of extracing mysql rows...

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    Toronto
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just curious, why is accessing the information as $myarray[#]['name'] an issue?


Bookmarks

Posting Permissions

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