简体   繁体   English

从CSV检索后如何为数组项分配专有名称

[英]how to assign proper names to array items after retrieving from CSV

Im creating an array from a CSV file in PHP. 我从PHP中的CSV文件创建数组。 However as expected the output is giving the an incrementation of numbers. 但是,正如预期的那样,输出给出的是数字的增量。

Here is my code 这是我的代码

        $file = @fopen('customers.csv', 'r');

    // Create arrays from the CSV file
    echo '<pre>';
        while (($line = fgetcsv($file)) !== FALSE) {

            $customers[] =$line;
        }
    echo '<pre>';
    print_r($customers);

This outputs. 这输出。

Array
(


[0] => Array
    (
        [0] => 148
        [1] => 0
        [2] => Adrian
        [3] => de Cleir


    )

[1] => Array
    (
        [0] => 305
        [1] => 0
        [2] => John 
        [3] => Smyth

    )

[2] => Array
    (
        [0] => 49
        [1] => 0
        [2] => adrian
        [3] => O'malley

    )
 )

when writing this information to the database it would be nicer not to be using the numbers.... 当将此信息写入数据库时​​,最好不要使用数字。

eg 例如

[0] => Array
    (
        [customer_id] => 148
        [store_id] => 0
        [first_name] => Adrian
        [second_name] => de Cleir


    )

In your while loop: 在您的while循环中:

while (($line = fgetcsv($file)) !== FALSE) {
    $customers[] = array(
        'customer_id' => $line[0],
        'store_id' => $line[1],
        'first_name' => $line[2],
        'second_name' => $line[3],
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM