Jump to content

explode and code help pls


lorien71

Recommended Posts

Hi , very new to php and having a little trouble posting so if I am repeating myself because the post that came up with an error message appears I do apologise! I am writing code to read the data in a text file <info.txt> This is a list of people with their name and favourite colours in the format                                              paul,blue
                                                       jon,red (etc etc)

I then have to print out the favourite colour of selected names.
I got this far
[code]<?php


$fave = file('info.txt');  //redirect file content into array

$faves_jon=$fave[1];         
$faves_bill=$fave[16];     
$faves_abby=$fave[18];      //assign name to element from array

$favourite_colour = array("$faves_jon","$faves_bill","$faves_abby");
// redirect chosen elements to an array called <$favourite_colour>

$n=0;
while($n<sizeof($favourite_colour)) {
$fd=explode(',' ,$favourite_colour[$n]);  //using explode to separate the elements of the array
print"$fd[0]'s favourite colour is $fd[1]<br>";       
$n++;         
}[/code]
This gives the output but seems a little clumsy as I feel I should be finding the elements rather than counting where they are (e.g jon is second in the text list) Secondly I then have to amend this code to count the names of all in the list. I did this by counting the elements in the original array but not the names
Hope this makes sense and I haven't worded it too badly
Many thanks
Lorien71(edited after reading 'how to post' sorry!)
Link to comment
https://forums.phpfreaks.com/topic/28668-explode-and-code-help-pls/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.