Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Multidimensional array from text file
Old 09-05-2007, 04:10 AM Multidimensional array from text file
Junior Talker

Posts: 2
Trades: 0
Hi all,

Im having a problem reading CSV data from a text file and storing it in a multidimensional array.

I have a text file like so.
Code:
Cathal|kyle@example.com|83.103.100.300|Wednesday 29th August 2007 @ 17:27-13|66023f4984
tony|tonto@example.com|83.107.82.300|Friday 31st August 2007 @ 12:46-49|1c3fd4594d
steve|imapisces@example.com|86.5.147.300|Friday 31st August 2007 @ 17:22-19|d61f895714
Code:
$file = 'file.txt';
$username = 'bob';

$array = file($file);

foreach($array as $key => $var){
    $var = explode('|', $var);
    if($username == $var[0]){
        $user = $var;
        break;
    }
}
if(!isset($user))
echo 'No user found';
else echo 'User found'; 


foreach ($var as $key => $value){
  print "<p>key = $key and value = $value</p>";
}
My foreach still just displays the last array (line of text from file)

I am trying very hard to work this one out myself, but I cant get my head around this one.

Hope you can understand what im trying to achive.
If this page receives the username via $_GET I want to find a way of having that username's line of info from the text file in an array for further manipulation.

Any help with this is greatly apreciated.

Regards Kyle

Last edited by homer2k; 09-05-2007 at 04:12 AM..
homer2k is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-05-2007, 05:23 PM Re: Multidimensional array from text file
Super Talker

Posts: 130
Trades: 0
try using fopen() http://us.php.net/manual/en/function.fopen.php
__________________
flann

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
flann is offline
Reply With Quote
View Public Profile
 
Old 09-06-2007, 10:52 PM Re: Multidimensional array from text file
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
You had the right idea going, but $var was being overwritten at each iteration.

PHP Code:
$file 'file.txt';
$username 'bob';
$array file($file);
 
foreach (
$array AS $var)
{
  
$var explode('|'$var);
  if (
$username == $var[0])
  {
    
// Assign username to $user
    
$user $var[0];
    
// Assign exploded array to $user_array
    
$user_array $var;
    break;
  }
}
 
echo (!isset(
$user)) ? 'No user found' 'User found';
 
// Print array values if user array is set
if (isset($user_array))
{
  foreach (
$user_array AS $key => $value)
  {
    echo 
"<p>key = $key and value = $value</p>";
  }

__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is online now
Reply With Quote
View Public Profile
 
Old 09-13-2007, 04:29 AM Re: Multidimensional array from text file
Junior Talker

Posts: 2
Trades: 0
Thanks for your help
homer2k is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Multidimensional array from text file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12921 seconds with 12 queries