johnwayne77 Posted December 4, 2008 Share Posted December 4, 2008 i have a list of fieldA and fieldB as following: "fieldA1","fieldB1" "fieldA2","fieldB2" "fieldA3","fieldB3" stored in fields.txt now i need to put fieldA and fieldB in its own variables as following: $fielda = "fieldA1"; $fieldb = "fieldB1"; then run a function as following: $function = function($fielda,$fieldb); then run an echo as following: echo $function; how do i extract all fieldA and fieldB fields from fields.txt, apply the function to obtain the echo and create a loop to do that for all existent fieldA and fieldB any ideas, logical algorithm or advices much appreciated. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/135555-solved-loop/ Share on other sites More sharing options...
rhodesa Posted December 4, 2008 Share Posted December 4, 2008 is the file a CSV file? replace functionName with the name of your function... <?php $filename='fields.txt'; $handle = fopen($filename,'r'); while (($data = fgetcsv($handle)) !== FALSE) { echo functionName($data[0],$data[1]); } @fclose($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/135555-solved-loop/#findComment-706159 Share on other sites More sharing options...
.josh Posted December 4, 2008 Share Posted December 4, 2008 Use file to get an array of lines, foreach to loop through each line, explode the line at the comma, trim the quotes off the values. Quote Link to comment https://forums.phpfreaks.com/topic/135555-solved-loop/#findComment-706169 Share on other sites More sharing options...
johnwayne77 Posted December 4, 2008 Author Share Posted December 4, 2008 any idea why i get the result: "Array" ? i tested with the fields.txt in following format: "fieldA1","fieldB1" "fieldA2","fieldB2" and : fieldA1,fieldB1 fieldA2,fieldB2 Quote Link to comment https://forums.phpfreaks.com/topic/135555-solved-loop/#findComment-706218 Share on other sites More sharing options...
rhodesa Posted December 4, 2008 Share Posted December 4, 2008 what gets you the result Array? echoing $data? $data is an array, so you can't just use echo on it. use print_r($data) Quote Link to comment https://forums.phpfreaks.com/topic/135555-solved-loop/#findComment-706315 Share on other sites More sharing options...
johnwayne77 Posted December 4, 2008 Author Share Posted December 4, 2008 thanks for the php lesson dude! it worked, now i just need to clean up the array results, pack it up and it's ready to go cheers and happy holidays! Quote Link to comment https://forums.phpfreaks.com/topic/135555-solved-loop/#findComment-706346 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.