The program is to enter two TXT files in JSON format and then output a new TXT file in JSON format by matching the same index of two files. Like what:
This is the format of the two files entered:
1. Product
{
"Product_Name": String//A unique ID for the product
"Manufacturer": String
"Family": String//optional grouping of products
"Model": String
"Announced-date": string//ISO-8601 formatted date String, e.g. 2011-04-28t19:00:00.000-05:00
}
2. Listing
{
"title": String//Description of product for sale
"Manufacturer": String//WHO manufactures the product for sale
"Currency": String//currency code, e.g. USD, CAD, GBP, etc.
' Price ': String//price, e.g. 19.99, 100.00
}
The file format for the output is:
{
"Product_Name": String
"Listings": array[listing]
}
First on the code:
baseitem.php
Buildfromarray ($fields); } /** * @param array $fields * /abstract Public Function Buildfromarray (array $fields); /** * @return Array * /abstract Public Function Converttoarray ();}? >
listing.php
$this->title, ' manufacturer ' = $this->manufacturer, ' currency ' and $this->currency, ' Price ' = $this->price,); }/** * @inheritDoc */Public Function Buildfromarray (array $fields) {$this->title = $fields [' Title ']; $this->manufacturer = $fields [' manufacturer ']; $this->currency = $fields [' Currency ']; $this->price = $fields [' Price ']; } public Function getcurrency () {return $this->currency; } public Function Getmanufacturer () {return $this->manufacturer; } public Function GetPrice () {return $this->price; } public Function GetTitle () {return $this->title; }}
product.php
ProductName = $fields [' product_name ']; $this->manufacturer = $fields [' manufacturer ']; $this->family = isset ($fields [' family '])? $fields [' family ']: null; $this->model = $fields [' model ']; $this->announceddate = $fields [' announced-date ']; }/** * @inheritDoc */Public Function Converttoarray () {return array (' Product_Name ' = = $this->productname, ' manufacturer ' = $this->manufacturer, ' family ' and $this->f amily, ' model ' = $this->model, ' announced-date ' and $this->announceddate,); } public Function Getannounceddate () {return $this->announceddate; } public Function getfamily () {return $this->family; } public Function Getmanufacturer () {return $this->manufacturer; } public Function Getmodel () {return $this->model; } public Function Getproductname () {return $this->productname; }}?>
prolistmatcher.php
Products = $this->normalizerawdata ($products, self::type_product); $this->listings = $this->normalizerawdata ($listings, self::type_listing); }/** * Matches up listings to Products * * @return array */Public function getmatches () { $ret = Array (); Loop through All products foreach ($this->products as $product) {//Reset matching Listings $matchingListings = Array (); Loop through all listings foreach ($this->listings as $key + $listing) {$matches = 0; Match on Manufacturer if (Strtolower ($product->getmanufacturer ()) = = Strtolower ($list Ing->getmanufacturer ())) {$matches + +; }//Match on the model in listing title $result = Strpos (Strtolower ($listing->gettitle ()), S Trtolower ($product->getmodel ())); if (false!== $result) { $matches + +; } if (2 = = = $matches) {$matchingListings [] = $listing; have been matched, remove listing from the future searches unset ($this->listings[$key]); }} if (count ($matchingListings)) {///1 or more matches were found, add to output $ret [] = Array (' product ' = = $product, ' listings ' = = $matching Listings); }} return $ret; }/** * Convert plain nested array to array of objects * * @param array $data * @param int $type * @return Array * @throws \invalidargumentexception */protected function Normalizerawdata (array $data, $type) {//build model for each item return Array_map (function ($value) use ($type) {switch ($type ) {Case Prolistmatcher::type_product:return new PRODUCT ($value); Case Prolistmatcher::type_listing:return New LISTING ($value); Default:throw New \invalidargumentexception (sprintf (' Type '%s ' is not valid ', $type)); }}, $data); }}
match.php
");} Array_shift ($ARGV);/** * @param string $data * @return array */function jsontoarray ($value) {$ret = explode ("\ n", Trim ($val UE)), Return Array_map (function ($value) {return Json_decode ($value, True);}, $ret);} $matcher = new Prolistmatcher (Jsontoarray (file_get_contents ($argv [0]), Jsontoarray (file_get_contents ($argv [1 ])), $matches = $matcher->getmatches (), $matches = Array_map (function ($value) {return Json_encode (' product_ Name ' = ' $value [' Product ']->getproductname (), ' listing ' = ' Array_map ' (function (listing $value) {return $value- >converttoarray (), $value [' listing '))), $matches), $output = fopen ("Output.txt", "w+"); File_put_contents ($ Output, implode ("\ n", $matches)); fclose ($output);? >
This is my code, and then I run it in Terminal:
Chrisfus-macbook-pro:test chrisfu$ php match.php products.txt listings.txt php Fatal Error: cannot redeclare class Li Sting in/users/chrisfu/sites/test/model/listing.php on line 53Fatal Error:cannot Redeclare class Listing In/users/chris fu/sites/test/model/listing.php on line 53
Why this error, I do not have two called listing class, please help to see, the program is posted up, we can run to try.
Reply to discussion (solution)
There are match.php in the
Include './model/listing.php ';
Include ' prolistmatcher.php ';
There are prolistmatcher.php in the
Include './model/product.php ';
Include './model/listing.php ';
Include './model/baseitem.php ';
is class Listing not defined repeatedly?
There are match.php in the
Include './model/listing.php ';
Include ' prolistmatcher.php ';
There are prolistmatcher.php in the
Include './model/product.php ';
Include './model/listing.php ';
Include './model/base ...
So, what if I need to use the two files in match.php?
Then you in product.php, listing.php How can use include _once ' baseitem.php ';
Once once
The include_once is loaded without loading
Then you in product.php, listing.php How can use include_once ' baseitem.php ';
Once once
The include_once is loaded without loading
Yes, I didn't pay much attention to the difference between include and include_once, and learned
LS also encountered the same problem, this is my solution: http://www.ibihuo.com/show-56.html