Re: key pairs

From: Date: Tue, 17 Dec 2002 20:37:26 +0000
Subject: Re: key pairs
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-128605@lists.php.net to get a copy of this message
This is a bit crude but should work: <?php $data = <<<EOT SHIP TO: Business Name: Contact Name: Ashley Kirchner Day Number: 303 442-6410 Evening Number: Fax Number: Address: 3550 Arapahoe Ave., Ste. #6 Address: Address: City: Boulder State/Province: CO Zip/Postal Code: 80303 Country: USA BILL TO: Business Name: Contact Name: Ashley M. Kirchner Day Number: 303 4426410 Evening Number: Fax Number: 303 442-9010 Address: 3550 Arapahoe Avenue Address: Suite #6 Address: City: Boulder State/Province: CO Zip/Postal Code: 80303 Country: USA EOT; $ship_to = array(); $bill_to = array(); $state = 0; // 0 = ship_to, 1 = bill_to function create_data ($s) { global $ship_to; global $bill_to; global $state; $s[1] = trim ($s[1]); $s[2] = trim ($s[2]); if (!strcmp ($s[1], 'SHIP TO')) $state = 0; elseif (!strcmp ($s[1], 'BILL TO')) $state = 1; else { if ($state) $bill_to[$s[1]] = $s[2]; else $ship_to[$s[1]] = $s[2]; } return $s[0]; } preg_replace_callback ("/(.*)\:[ ]*(.*)?[\n]?/", "create_data", $data); print_r ($ship_to); print_r ($bill_to); ?> Note that I made a bunch of assumptions here--that the data is always properly formatted, that there is only one record in the $data, and that you will later manipulate the key names so that they will actually work as database columns. Still, this way you can manipulate each value directly in the function, which is something that I always find very practical, so you can add whatever additional checks need to be performed. Hope this helps! Cheers, Marco -- ------------ php|architect - The magazine for PHP Professionals The monthly worldwide magazine dedicated to PHP programmers Come visit us at http://www.phparch.com!

« previous php.general (#128605) next »