|
-
Jul 20th, 2010, 10:51 PM
#1
Thread Starter
Frenzied Member
loading text file into array help
Hi all; It's been quite a few years since I've visited these forums! Surprised my login was correct on the first attempt..
I haven't done any sort of programming in years, but just got into a little bit of PHP for fun over the past month or so. I have an issue right now and I don't know if I'm going about this right or not, and I found another way to accomplish this but it would involve just using someone else's code which I'd rather try and keep going in the direction I've already gone with this.
OK: I have multiple files involved here. an html form for user input, and then a php file for grabbing the info and putting it all in a text file on new lines. My issue now is for my 2nd php file I want to grab the text back from the text file and put it into an array.
Heres an example of what my text file would look like:
last name
first name
password
email
phone
experience level
last name
first name
password
email
phone
experience level
As you can see it will just keep repeating like above, depending on how many entries were on the form.
So what I'm trying to do is grab the first 6 lines(before they reset to the next user in the file) and place into a multidimensional array. then the next 6 lines..etc
I want to be able to then display this information into a table. (which I haven't gotten to this part yet..but doubt ill have too much trouble here)
PHP Code:
$myUser = array("Green" => array(
"first name" => "Tom",
"password" => "Ha",
"email" => "[email protected]",
"phone" => "1234567",
"experience" => "Advanced"
this is sort of the format I'd go about for the array I'm thinking. I've tried several things with counters etc, but if anyone has an example or easier way to grab my text file and put into a multidimensional array without knowing how much data will be in the file.
I've used the following to grab the data from each line and place it into an array, but just not a multiD array.
PHP Code:
$File = file_get_contents("users.txt");
$myArray = explode("\n", $File);
Any help or insight would be appreciated!
Thanks in advance.
~
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 21st, 2010, 12:22 PM
#2
Thread Starter
Frenzied Member
Re: loading text file into array help
A little update here
I took a short cut since I wasn't having any luck with getting my info into a multi-dimensional array(for sorting by certain values). I just went ahead and used a regular index array like mentioned above with the explode function to grab each line in the text file into an element of the array.
Heres my code I used to grab the information and display in a table(had to use a counter to go to the next row after the first 6 pieces of info was grabbed).
PHP Code:
$File = file_get_contents("users.txt"); $myArray = explode("\n", $File); $counter = 0; echo "<table width='100%' border='1'>"; echo "<tr><th>Last Name</th><th>First Name</th><th>Password</th><th>Email</th><th>Phone Number</th><th>Experience Level</th></tr>"; echo "<tr>"; for($i=1; $i < count($myArray); $i++) { echo "<td>$myArray[$i]</td>"; $counter++; if($counter == 6) { echo "</tr>"; echo "<tr>"; $counter = 0; } }
This works fine for printing out my information. However, now because of going this route, I have no way of sorting the information based on Last Name, or any other keys. Any ideas of how I can go about doing that at this point, or am i going to have to go back and re-think my method of getting the data into the array first.
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 21st, 2010, 12:39 PM
#3
Thread Starter
Frenzied Member
Re: loading text file into array help
Been searching around and I should be able to keep the code that I have. I will just need to still find a way to explode my information from the text file into a multidimensional array properly, then I can sort it and overwrite the text file with the new sorted data..and just reopen using the code I've already done above.
So far I haven't been able to find any code or information on creating a multidimensional array from a text file or a string...none with the limitations i have. E.g. I need every 6 lines to be their own portion of the array, last name being the primary key as such..
last name => first name => password => email => phone => experience
then after those first 6 lines are put into the array, it will start over with the next 6 lines but maintaining the same format, just using the next users information.
last name => first name => password => email => phone => experience
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 21st, 2010, 03:03 PM
#4
Re: loading text file into array help
this?
PHP Code:
$sortable = array();
for($i = 0; $i < count($array); $i += 6){
$sortable[$array[$i]] = array("firstname" => $array[$i + 1] , "password" => $array[$i + 2] , "email" => $array[$i + 3] , "phone" => $array[$i + 4] , "experience" => $array[$i + 5]);
}
-
Jul 21st, 2010, 09:13 PM
#5
Thread Starter
Frenzied Member
Re: loading text file into array help
Thanks for the reply kows. That looks like it might work with a little more tweaking. So far I've been getting Undefined offset errors with it.
Notice: Undefined offset: 31 in C:\wamp\www\userlists.php on line 18
Notice: Undefined offset: 32 in C:\wamp\www\userlists.php on line 19
Notice: Undefined offset: 33 in C:\wamp\www\userlists.php on line 20
Notice: Undefined offset: 34 in C:\wamp\www\userlists.php on line 21
Notice: Undefined offset: 35 in C:\wamp\www\userlists.php on line 22
Notice: Undefined offset: 36 in C:\wamp\www\userlists.php on line 23
As I understand it, with PHP, this is similar to an undefined variable error in other languages?
PHP Code:
for($i = 0; $i < count($myArray); $i += 6){
$sortable[$myArray[$i]] = array("lastName" => $myArray[$i + 1] , "firstName" => $myArray[$i + 2] , "email" => $myArray[$i + 3] , "phone" => $myArray[$i + 4] , "experience" => $myArray[$i + 5]); } for($i = 0; $i < count($sortable); $i++) { echo $sortable[$i]; }
It seems to be having the issue with my $i I think. perhaps i'm passing in the myArray incorrectly, or if I can just use the explode function with the above code instead and just go straight from my text file, since it's line by line.
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 21st, 2010, 10:39 PM
#6
Re: loading text file into array help
in the code I gave you, $array should be an array holding every line of whatever file you're working with. so:
PHP Code:
$array = file("myfile.txt");
// .. the rest of my code
in your for() loop, you are echoing an array (which you can't really do). replace your entire loop with a call to print_r() instead:
PHP Code:
print_r($sortable);
edit: oh yeah, and if you continue to get that error, basically it's just saying that those indexes don't exist (as in, you're looping too far). try <= count($array) instead of <.
Last edited by kows; Jul 21st, 2010 at 10:45 PM.
-
Jul 21st, 2010, 11:10 PM
#7
Thread Starter
Frenzied Member
Re: loading text file into array help
$myArray is the array thats holding all the information from my text file.
Still getting the undefined errors, however the print_r statement looks like the array is holding all of the information correctly, so not sure why it's giving the errors still even after changing to <= count .
Not sure if I can just use @ to ignore the errors or not, and just sort the array and dish it back into my text file again, 1 criteria per line.
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 21st, 2010, 11:18 PM
#8
Thread Starter
Frenzied Member
Re: loading text file into array help
Found this bit of code snippet that I may try and implement if I have to..
http://www.phptoys.com/e107_plugins/...php?content.58
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 22nd, 2010, 12:35 AM
#9
Re: loading text file into array help
if you're still getting the errors, then it just means that you have an extra line at the end, or something. you can just be defensive and check to see if those indexes exist:
PHP Code:
$sortable = array();
for($i = 0; $i <= count($array); $i += 6){
// break the loop if any of these do not exist if(!isset($array[$i], $array[$i + 1], $array[$i + 2], $array[$i + 3], $array[$i + 4], $array[$i + 5])) break;
$sortable[$array[$i]] = array("firstname" => $array[$i + 1] , "password" => $array[$i + 2] , "email" => $array[$i + 3] , "phone" => $array[$i + 4] , "experience" => $array[$i + 5]);
}
-
Jul 22nd, 2010, 12:43 AM
#10
Thread Starter
Frenzied Member
Re: loading text file into array help
Thanks, that worked like a charm. 
Now I'm just going to work on sorting and removing dups.
Quick question regarding the array_unique() functions; I assume that I will need to modify that a bit to remove just duplicates based on just last name, first name.
In it's current form, from how i'm understanding it to work, it'd remove any duplicates in the entire array (phone, email, etc) which I don't want.
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 22nd, 2010, 02:34 AM
#11
Re: loading text file into array help
for the record -- array_unique() is not recursive and isn't really intended to be used for multidimensional arrays. it only checks against the value of the key, and so shouldn't remove anything unless it's an exact duplicate (at least, that's how it should work). because of the way array_unique() works, though, you might get unexpected results. array_unique() typecasts values to strings, and if an array typecast to a string is "Array" (as it is when you try echoing an array), then all of your values would be seen as duplicates, because they are all arrays. I would suggest just not using this function.
however, that really doesn't matter. you actually won't be able to have duplicates, anyway -- you are using the last name as the key in the array. if John Smith is the first name in your list, then $sortable['Smith'] is set. but then Kim Smith later comes on, so $sortable['Smith'] is overwritten with Kim's data.
Last edited by kows; Jul 22nd, 2010 at 02:37 AM.
-
Jul 22nd, 2010, 10:33 AM
#12
Thread Starter
Frenzied Member
Re: loading text file into array help
Got everything working now. Just a simple sort($sortable); worked fine for sorting by my main key's values.
thanks again for the help. i'm sure i'll be back when i move on to another project and get stuck
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 22nd, 2010, 10:53 AM
#13
Thread Starter
Frenzied Member
Re: loading text file into array help
1 more thing before I go reading up on all the possible functions that may help.
is there a function that i can use sort of similiar to array_search() if i want to do the following:
I've made 3 seperate counters.
PHP Code:
counterString1 = 0; counterString2 = 0; counterString3 = 0;
I want to search lets say the experience key.. $sortable['experience'] for 3 possible strings, not exact matches though; needs to look for partial within the key values. Because 'experience' could contain 1, 2, or all 3 strings in it. Obviously my counters will need to go up every time one of those strings is found within the 'experience' key's values.
At first I was messing around before realizing something like this would need to be partial..but maybe im on the right track.
PHP Code:
foreach($sortable as $item) { if($item['experience'] CONTAINS "string 1") { counterString1++; } if($item['experience'] CONTAINS "string 2") { counterString2++; } if($item['experience'] CONTAINS "string 3") { counterString3++; } }
The above code is what I'd LIKE to be able to do. if only there were a CONTAINS function that would work that easily.
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
-
Jul 22nd, 2010, 11:55 AM
#14
Thread Starter
Frenzied Member
Re: loading text file into array help
Got it.
found the preg_match() function and got that to work out for me.
Can post code if anyone else needs to see.
PHP Code:
foreach($sortable as $item)
{
if(preg_match("/STRING1/", $item['myKEY']))
{
$counterString1++;
}
if(preg_match("/STRING2/", $item['myKEY']))
{
$counterString2++;
}
if(preg_match("/STRING3/", $item['myKEY']))
{
$counterString3++;
}
}
:::`DISCLAIMER`:::
Do NOT take anything i have posted to be truthful in any way, shape or form.
Thank You!
--------------------------------
"Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
"Finaly I can look as gay as I want..." - NoteMe
Languages: VB6, BASIC, Java, C#. C++
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|