我需要从一个看起来像这样的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']标签都不能有括号,代码周围也没有括号。你能告诉我我做错了什么,如果有一个不同的阵列创建方法我需要使用,那是什么?谢谢。
答案 0 :(得分:0)
似乎更改了这个:
$responseMessages[$code]['body'] = $upc;
到此:
{{1}}
让它发挥作用。