michaellunsford Posted January 13, 2007 Share Posted January 13, 2007 I've been digging through the manual and I just can't find what I'm looking for ??? I know it's in there somewhere. I already have my own slow function to do this, but I was hoping one of you guys (or gals) could remind me how to do it with PHP's built in stuff.CSV file (tab separated). The top line contains the column names, each consecutive row contains data. I'd like to use the top row for the array key when I grab the other rows. I'm currently using fopen() and fgets() to explode() each line and loop through each key and value from the separate arrays and create a third array that's formatted properly. I'm not loading the entire file into memory -- just working one line at a time.What function or combination of functions does this already?Thanks! Quote Link to comment Share on other sites More sharing options...
Barand Posted January 13, 2007 Share Posted January 13, 2007 are you thinking of array_combine() ? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 13, 2007 Share Posted January 13, 2007 If I am understanding you correctly I would suggest the following:When grabbing the first line, explode it into an array and save to a variable (ex. $keys).Then create a loop to grab each succesive line and explode into an array (ex. $values)Then add the $values array, using the $keys array, to a results array (ex. $results) using array_combine()$results[] = array_combine($keys, $values); Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 13, 2007 Author Share Posted January 13, 2007 Kind of the long way around, I could probably combine a few steps[code]<?php$keys=explode("\t",$line);$myarray=array_combine($keys,explode("\t",fgets($fil)));?>[/code]only one problem... Still running PHP4 on the server :( Quote Link to comment Share on other sites More sharing options...
Barand Posted January 13, 2007 Share Posted January 13, 2007 try[code]<?phpfunction array_combine4 ($a, $b) { $res = array(); $v = current($b); foreach ($a as $k) { $res[$k] = $v; $v = next($b); } return $res;}?>[/code] Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 14, 2007 Author Share Posted January 14, 2007 Thanks. 8)Maybe one day I'll pay my ISP to upgrade me to PHP5. Just can't figure it out :( Quote Link to comment Share on other sites More sharing options...
MCP Posted January 14, 2007 Share Posted January 14, 2007 To ease your future migration process, you might want to do this: (it's Barand's function, just renamed and wrapped in an if). This way when you upgrade to a version that has array_combine, then it should work seamlessly.[code=php:0]if (!function_exists('array_combine')) { function array_combine ($a, $b) { $res = array(); $v = current($b); foreach ($a as $k) { $res[$k] = $v; $v = next($b); } return $res; }}[/code] Quote Link to comment 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.