【问题标题】:How do i create this php array from this csv?我如何从这个 csv 创建这个 php 数组?
【发布时间】:2018-04-26 20:41:23
【问题描述】:

我需要从一个如下所示的 csv 文件创建一个数组。

$responseMessages = array(
    '111111'    => array('body' => 'test me, We will give you up to 3001 for your 2010 toyota camry. Visit http://testme.whatever.com or call +17777777777'),
    '222222'    => array('body' => 'test you, We will give you up to 3002 for your 2011 nissan maxima. Visit http://testyou.whatever.com or call +17777777777'),
    '333333'    => array('body' => 'test him, We will give you up to 3003 for your 2012 honda civic. Visit http://testhim.whatever.com or call +17777777777'),
    '444444'    => array('body' => 'test her, We will give you up to 3004 for your 2013 hyundai sonata. Visit http://testher.whatever.com or call +17777777777'),
    '555555'    => array('body' => 'test them, We will give you up to 3005 for your 2014 subaru legacy. Visit http://testthem.whatever.com or call +17777777777'),
    '666666'    => array('body' => 'test us, We will give you up to 3006 for your 2015 acura integra. Visit http://testus.whatever.com or call +17777777777')
);

csv 看起来像这样:

first   last    year    make    model   value   code    url
test    me      2010    toyota  camry   3001    111111  http://testme.whatever.com
test    you     2011    nissan  maxima  3002    222222  http://testyou.whatever.com
test    him     2012    honda   civic   3003    333333  http://testhim.whatever.com
test    her     2013    hyundai sonata  3004    444444  http://testher.whatever.com
test    them    2014    subaru  legacy  3005    555555  http://testthem.whatever.com
test    us      2015    acura   integra 3006    666666  http://testus.whatever.com

这是我正在使用的 php:

$phone = "+17777777777";
$handle = fopen("test.csv", "r");
$row = 0;
$responseMessages = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE && $row < 8) {
    if ($data[6] !== "code") { //leave out header line
        $code = $data[6];
        $upc = $data[0]." ".$data[1].", We will give you up to ".$data[5]." for your ".$data[2]." ".$data[3]." ".$data[4].". Visit ".$data[7]." or call ".$phone;
        $responseMessages['\''.$code.'\'']['\'body\''] = '\''.$upc.'\'';
        $row++;
    }
}
echo "<pre>";
print_r( $responseMessages );
echo "</pre>";

这是正在生成的数组:

Array
(
    ['111111'] => Array
        (
            ['body'] => 'test me, We will give you up to 3001 for your 2010 toyota camry. Visit http://testme.whatever.com or call +17777777777'
        )

    ['222222'] => Array
        (
            ['body'] => 'test you, We will give you up to 3002 for your 2011 nissan maxima. Visit http://testyou.whatever.com or call +17777777777'
        )

    ['333333'] => Array
        (
            ['body'] => 'test him, We will give you up to 3003 for your 2012 honda civic. Visit http://testhim.whatever.com or call +17777777777'
        )

    ['444444'] => Array
        (
            ['body'] => 'test her, We will give you up to 3004 for your 2013 hyundai sonata. Visit http://testher.whatever.com or call +17777777777'
        )

    ['555555'] => Array
        (
            ['body'] => 'test them, We will give you up to 3005 for your 2014 subaru legacy. Visit http://testthem.whatever.com or call +17777777777'
        )

    ['666666'] => Array
        (
            ['body'] => 'test us, We will give you up to 3006 for your 2015 acura integra. Visit http://testus.whatever.com or call +17777777777'
        )

)

看起来不错,但不是真的。不确定我是否需要每个数组之间的逗号,或者单词数组是否需要小写,但主要是所有 ['body'] 标记都不能有括号,代码周围也没有括号。你能告诉我我做错了什么吗,如果我需要使用不同的数组创建方法,那是什么?谢谢。

【问题讨论】:

  • 这两个输出看起来和我一模一样。有什么不同?
  • eval.in/995596 您的输出与您提供的 $responseMessages 数组相同。只是格式有点不同 - print_r() 格式化事物的结果,而不是您将它们编写为代码的方式。
  • 好吧,当我使用代码访问应用程序时,它无法使用 csv 方法正确响应,就像我自己创建数组时一样,但如果 csv 有 10,000 行,则不会执行10,000 行 php 数组。这就是为什么我需要一个应用程序需要的正确格式的自动阵列制造商。
  • 使用 twilio 服务和这个确切的例子,减去媒体(图像)。 support.twilio.com/hc/en-us/articles/… ... 基本上需要使用相同的代码,但从 csv 文件中读取,而不是从所有不同代码和正文组合的硬编码 php 数组中读取。
  • 我不确定我是否理解你的意思。在您说的问题中“这是正在生成的数组:”。该数组(我假设它是您上面代码的输出)与您在问题中定义为 $responseMessages 的示例数组完全相同。您说您想创建一个与 $responseMessages 相同的数组,而您的代码已经这样做了。因此我们不得不假设它工作正常。如果您说它没有正常工作,您将不得不更详细地解释,因为根据您问题中的信息,似乎一切正常。

标签: php arrays csv


【解决方案1】:

好像要改这个:

$responseMessages['\''.$code.'\'']['\'body\''] = '\''.$upc.'\'';

到这里:

$responseMessages[$code]['body'] = $upc;

成功了。

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 2014-03-23
    • 2016-02-18
    • 1970-01-01
    • 2017-02-03
    • 2019-02-23
    • 1970-01-01
    • 2017-03-17
    相关资源
    最近更新 更多