Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yorickke · Jun 02, 2012 at 10:38 AM · c#php2d array

Put user list in 2d array

Hello,

For a current application I am working on, I want to retrieve a list of users from a php website. The website currently displays the following as a user list:

Name1 | Email1 | Status1 | Name2 | Email2 | Status2 | Name3 | Email3 | Status3 |

Now what I would like to achieve is to put this information in an array, so I could just print ( for example ) the email adress of user 2.

I am able to put it into a built in array using the following:

 public string[]        userInfo;
 
 WWW hs_get = new WWW( post_url );     
 userInfo = hs_get.text.Split("|"[0]);     // put the information of the user in an array


It currently puts every word before a "|" into a new row.

I've been busy trying to use a 2d array, but I'm new to this and I cannot seem to figure it out. I have the following so far:

 string[ , ] userInfo = new string[3,3];
    WWW hs_get = new WWW( post_url );  

 

I have no idea how to put the hs_get.text into this array now. It would be great to make it the following:

 userInfo[0,1] = Name1, userInfo[0,2] = Email1, userinfo[0,3] = Status1
 userinfo[1,1] = Name2, userInfo[1,2] = Email2, userInfo[1,3] = Status2

etc.

I have no idea if I'm on the right track or if this is the best way to do this, I'm fairly new with all this.

I hope I explained my problem well enough since english is not my native language.

Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Jun 02, 2012 at 10:45 AM

You could put your name etc into a struct and just read the data into that - would be much easier to follow after:

 public struct UserInfo
 {
     public string name;
     public string email;
     public string status;
     
 }
 
 public UserInfo []  userInfo;

 public void GetData(string post_url)
 {
     
     WWW hs_get = new WWW(post_url);   
     var data = hs_get.text.Split("|"[0]);
             var numUsers : int = Mathf.Floor(data.Length/3);
     userInfo = new UserInfo[numUsers];
     
     for (int i = 0, user = 0; user < numUsers && i+2 < data.Length; i+=3, user++)
     {
         userInfo[user].name = data[i];
         userInfo[user].email = data[i + 1];
         userInfo[user].status = data[i + 2];
     }
     
     
     
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yorickke · Jun 02, 2012 at 08:06 PM 0
Share

Hey $$anonymous$$ike, thanks for your answer. Hadn't heard of struct yet, but as far as I understand it, it seems like a great sollution. I've used your code. I do get an error tho.

 IndexOutOfRangeException: Array index is out of range.

It has something to do with the "userInfo[user].name = data[i];" line, I did a bit of trial & error the past 30 $$anonymous$$, but I can't seem to figure out what the problem is. Do you perhaps have an idea about what might be wrong?

avatar image whydoidoit · Jun 02, 2012 at 08:08 PM 0
Share

Sounds like there isn't an even group of three of the data elements co$$anonymous$$g in. Will modify my answer to account for that eventuality.

avatar image whydoidoit · Jun 02, 2012 at 08:11 PM 0
Share

Updated. BTW please use the add new comment link hidden at the right of the screen rather than new answers when making a comment :).

Edit

For some reason I can't post another comment! Per @mdreptile (below) the answer is updated to force numUsers to be an int. thx

avatar image yorickke · Jun 03, 2012 at 11:12 AM 0
Share

Hey $$anonymous$$ike, thanks for the update.

Somehow the line

"userInfo = new UserInfo[numUsers];"

doesn't work. I get the following error:

"Cannot implicitly convert type float' to int'. An explicit conversion exists"

When I make the line for example, it does work: "userInfo = new UserInfo[3];"

And when I print the following, I do get an int: "var numUsers = $$anonymous$$athf.Floor(data.Length/3);"

So I don't understand why numUsers isn't allowed in the UserInfo[].

Thanks for helping me.

avatar image MD_Reptile · Jun 03, 2012 at 11:18 AM 1
Share

this must mean userInfo is an integer, while numUsers in a float I believe. (ignore bad advice following) You could use $$anonymous$$ath.RoundToInt() to convert the float to an integer, and then set the userInfo to that int.

something like:

int numUserInt = $$anonymous$$athf.RoundToInt(numUsers);

then, change to this:

userInfo = numUserInt;

(/end bad advice)

Or actually... you could in javascript do it different, sorry im used to C#...

just when you set "var numUsers ..." ins$$anonymous$$d do "var numUsers : int =..." to specifically set it as an integer, again im not so hot with unity javascript so I dont know if thats how its done exactly :P but I think it is assu$$anonymous$$g a float for that var, so you need to specify that it is a integer ins$$anonymous$$d. Im basing this all on theory I might be completely wrong, but if that doesnt work perhaps someone will come along and correct me!

@whydoidoit no problem hope things work for you yorickke

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to send data form to server using json in unity 1 Answer

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Why the Value of form.AddField won't accept string 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges