当前位置:Gxlcms > PHP教程 > 请大家帮忙看看问题

请大家帮忙看看问题

时间:2021-07-01 10:21:17 帮助过:3人阅读

程序是要输入两个json格式的txt文件,然后通过匹配两个文件的相同的index,然后输出一个新的json格式的txt文件。 比如:
这是输入的两个文件的格式:
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
}

输出的文件格式为:
{
"product_name": String
"listings": Array[Listing]
}

先上代码:
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' => $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' => $this->family,            'model' => $this->model,            'announced-date' => $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($listing->getManufacturer())) {                    $matches++;                }                // match on model in listing title                $result = strpos(strtolower($listing->getTitle()), strtolower($product->getModel()));                if (false !== $result) {                    $matches++;                }                if (2 === $matches) {                    $matchingListings[] = $listing;                    // has been matched, remove listing from future searches                    unset($this->listings[$key]);                }            }            if (count($matchingListings)) {                // 1 or more matches were found, add to output                $ret[] = array(                    'product' => $product,                    'listings' => $matchingListings                );            }        }        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($value));	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(array(		'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);?>


这是我的代码,然后我在terminal运行:
chrisfus-MacBook-Pro:test chrisfu$ php Match.php products.txt listings.txt PHP Fatal error:  Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53Fatal error: Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53

为什么会出现这样的错误,我没有两个叫Listing的 class,请大家帮忙看看,程序都贴上去了,大家可以运行试试。


回复讨论(解决方案)

Match.php 中有
include './model/Listing.php';
include 'ProListMatcher.php';

ProListMatcher.php 中有
include './model/Product.php';
include './model/Listing.php';
include './model/BaseItem.php';

class Listing 不是被重复定义了吗?

Match.php 中有
include './model/Listing.php';
include 'ProListMatcher.php';

ProListMatcher.php 中有
include './model/Product.php';
include './model/Listing.php';
include './model/Base……

原来是这样的,那如果我在Match.php需要用到那两个文件,是用use吗?

那你在 Product.php、Listing.php 就怎么会用 include _once 'BaseItem.php';

once 一次
include_once 已加载了就不加载

那你在 Product.php、Listing.php 就怎么会用 include_once 'BaseItem.php';

once 一次
include_once 已加载了就不加载

对的,以前没太注意include和include_once的区别,学习了

LS也遇到过相同问题,这是我的解决方法: http://www.ibihuo.com/show-56.html

人气教程排行