duanjue9296 2018-04-26 20:41
浏览 42
已采纳

我如何从这个csv创建这个PHP数组?

I need to create an array from a csv file that looks like this.

$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')
);

The csv looks like this:

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

This is the php i'm using:

$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>";

This is the array that is being produced:

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'
        )

)

Kind of looks right, but not really. Not sure if i need the commas that are between each array, or if the word array needs to be lowercase, but the main thing is all the ['body'] tags can't have brackets, and no brackets around the codes either. Can you tell me what i'm doing wrong, and if there is a different array creation method I need to be using, and what that is? Thank you.

  • 写回答

1条回答 默认 最新

  • doushang8846 2018-04-27 13:23
    关注

    Seems like changing this:

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

    to this:

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

    made it work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统