如何从这个csv创建这个php数组?
我需要从csv文件中创建一个数组,如下所示如何从这个csv创建这个php数组?,php,arrays,csv,Php,Arrays,Csv,我需要从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' =&
$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>";
看起来不错,但不是真的。我不确定是否需要每个数组之间的逗号,或者单词数组是否需要小写,但主要的是所有的['body']标记不能有括号,代码周围也不能有括号。你能告诉我我做错了什么吗?如果我需要使用不同的数组创建方法,那是什么?谢谢。似乎要改变这一点:
$responseMessages[$code]['body'] = $upc;
为此:
使它工作。这两个输出对我来说是一样的。有什么不同?您的输出与您提供的$responseMessages数组相同。只是格式有点不同,这是print\u r如何格式化的结果,而不是如何编写代码。好吧,当我用代码点击应用程序时,它不会正确地使用csv方法进行响应,就像我自己创建数组时一样,但是如果csv有10000行,就不会使用10000行php数组。这就是为什么我需要一个应用程序需要的正确格式的自动阵列生成器。使用twilio服务和这个确切的示例,减去媒体图像。。。基本上需要使用相同的代码,但从csv文件中读取,而不是从硬编码的php数组中读取所有不同的代码和正文组合。我不确定我是否理解你的意思。在问题中,您说这是正在生成的数组:。我假设上面代码的输出数组与您在问题中定义为$responseMessages的示例数组完全相同。您说过要创建一个与$responseMessages相同的数组,而您的代码已经这样做了。因此,我们必须假设它工作正常。如果你说它工作不正常,你必须更详细地解释,因为根据你问题中的信息,似乎一切都正常。
$responseMessages['\''.$code.'\'']['\'body\''] = '\''.$upc.'\'';
$responseMessages[$code]['body'] = $upc;