我在哪里可以存储数据库中每行更改的数组?

时间:2014-09-07 17:33:23

标签: php mysql laravel

我对Laravel以及PHP一般都是新手。以下代码正在运行,但我希望能够根据传递给parseXML函数的id更改xml数据解析到数组的方式。

<?php namespace Podguest\Feed;

use Podguest\Feed\TimeFormat;

class FeedConnect {

    public function fetch($url)
    {
        $ch = curl_init();

        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $output = curl_exec($ch);

        curl_close($ch);

        $this->xml = new \SimpleXMLElement ($output);

        return $this->xml;

    }

    public function parseXML($xml, $id)
    {
        $data = [];
        $i = 0;

            foreach ($xml->channel->item as $item) {
            $namespace = $item->getNameSpaces(true);
            $itunes = $item->children($namespace['itunes']);

            $data[$i++] = [
                'number' => preg_replace('/^.*#|\s-.*/', '', (string)$item->title),
                'title' => (string)$item->title,
                'pub_date' => date_format(date_create_from_format('D, d M Y H:i:s O', (string)$item->pubDate),'m/d/Y'),
                'duration' => TimeFormat::convertSeconds((string)$itunes->duration),
                'description' => (string)$item->description,
                'guests' => preg_replace('/^.* - |,? &.*|, \bme\b/i', '', (string)$item->title),
                'url' => (string)$item->link

            ];
    }   

        return $this->data = $data;
    }

}

我不知道最好的办法是什么。我想将它们存储在mySQL数据库中,但这不是我读过的关于eval()的好主意。我有什么其他选择?

2 个答案:

答案 0 :(得分:0)

编辑:更改功能以回答评论中提到的内容

您可以将此函数的格式化部分提取为新函数,并在调用该函数之前决定如何格式化XML。我添加了一个布尔参数,当您格式化特殊XML时,该参数必须为true。

警告:未经测试的代码如下......

public function parseXML($xml, $id, $specialXML = false)
{

    $i = 0;
    $data = [];

    foreach ($xml->channel->item as $item)
    {
        $namespace = $item->getNameSpaces(true);
        $itunes = $item->children($namespace['itunes']);

        if($specialXML == true)
        {
            $data[$i][] = $this->XMLToArrayFormatter($item);
        } else {
            $data[][$i++] = $this->XMLToArrayFormatter($item);
        }

     }   

      return $this->data = $data;
}

protected function XMLToArrayFormatter()
{
            return [
                'number' => preg_replace('/^.*#|\s-.*/', '', (string)$item->title),
                'title' => (string)$item->title,
                'pub_date' => date_format(date_create_from_format('D, d M Y H:i:s O', (string)$item->pubDate),'m/d/Y'),
                'duration' => TimeFormat::convertSeconds((string)$itunes->duration),
                'description' => (string)$item->description,
                'guests' => preg_replace('/^.* - |,? &.*|, \bme\b/i', '', (string)$item->title),
                'url' => (string)$item->link

            ];
 }

答案 1 :(得分:0)

您可以在PHP中使用多维数组:

$this->data[$id][$i++] = ...

每个$ id都有自己的项目列表。你应该将它存储在你的$ data属性中。

所以你的代码看起来像:

<?php namespace Podguest\Feed;

use Podguest\Feed\TimeFormat;

class FeedConnect {

    protected $data = [];

    public function fetch($url)
    {
        $ch = curl_init();

        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $output = curl_exec($ch);

        curl_close($ch);

        $this->xml = new \SimpleXMLElement ($output);

        return $this->xml;

    }

    public function parseXML($xml, $id)
    {
        $i = 0;

        foreach ($xml->channel->item as $item) 
        {
            $namespace = $item->getNameSpaces(true);

            $itunes = $item->children($namespace['itunes']);

            $this->data[$id][$i++] = [
                'number' => preg_replace('/^.*#|\s-.*/', '', (string)$item->title),
                'title' => (string)$item->title,
                'pub_date' => date_format(date_create_from_format('D, d M Y H:i:s O', (string)$item->pubDate),'m/d/Y'),
                'duration' => TimeFormat::convertSeconds((string)$itunes->duration),
                'description' => (string)$item->description,
                'guests' => preg_replace('/^.* - |,? &.*|, \bme\b/i', '', (string)$item->title),
                'url' => (string)$item->link

            ];
        }   

        return $this->data;
    }

}