Omzy Posted July 21, 2010 Share Posted July 21, 2010 I've got some data which I need to populate into a database. Currently it is the following form: East Kilbride = 10, Ellesmere Port = 1, Worksop = 6, Worthing = 4, Wrexham = 11, Yeovil = 3, York = 8, (that is just a sample of the data, there are about 400 rows) So it's basically like a key & value format, I can of course do a find&replace on the equals symbol to get the => operator and convert the data in to an array, but the main diffculty is to get the keys/values enclosed in single quotes. The idea is to build an mysql insert statement so that I can input all the data in to one of the tables. How would I go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/208474-convert-raw-text-data-into-a-mysql-insert-statement/ Share on other sites More sharing options...
dezkit Posted July 21, 2010 Share Posted July 21, 2010 well first you can explode() them by comma, then explode() them by = Quote Link to comment https://forums.phpfreaks.com/topic/208474-convert-raw-text-data-into-a-mysql-insert-statement/#findComment-1089328 Share on other sites More sharing options...
Omzy Posted July 21, 2010 Author Share Posted July 21, 2010 OK I've tried this, the thing is that the explode() function creates an array with it's own numerical indexes and that makes it more difficult to format the data. I put the data in to a string ($towns) and then did the following: $townarray=explode(",", $towns); $townstring=implode(",", $townarray); $newtownarray=explode("=", $townstring); foreach($newtownarray as $key=>$value) { echo $value; echo "\n"; } I think there is obviously an error in this and I can't figure out how to do it.. Quote Link to comment https://forums.phpfreaks.com/topic/208474-convert-raw-text-data-into-a-mysql-insert-statement/#findComment-1089336 Share on other sites More sharing options...
Omzy Posted July 22, 2010 Author Share Posted July 22, 2010 Anyone able to advise? Quote Link to comment https://forums.phpfreaks.com/topic/208474-convert-raw-text-data-into-a-mysql-insert-statement/#findComment-1089451 Share on other sites More sharing options...
dezkit Posted July 22, 2010 Share Posted July 22, 2010 $array = explode(",",$str); foreach($array AS $arr){ $arr = explode("=",$arr); echo $arr[0]." - ".$arr[1]."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/208474-convert-raw-text-data-into-a-mysql-insert-statement/#findComment-1089460 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.