php str_getcsv 数组问题

发布于 2024-12-15 14:54:28 字数 1882 浏览 0 评论 0原文

我正在上传 csv 文件,然后使用 str_getcsv 解析它。一切都很好,只是我需要一种方法来循环它们。理想情况下,如果数组返回并看起来像这样,那就太好了:

Array (      
    [1] => Array
       (
            [0] => 1 // first id in csv
            [1] => name
            [2] => address
            [3] => town
            [4] => state
            [5] => zip
            [6] => phone
            [7] => website
            [8] => other
            [9] => other
        )
    [22] => Array
       (
            [10] => name
            [11] => address
            [12] => town
            [13] => state
            [14] => zip
            [15] => phone
            [16] => website
            [17] => other
            [18] => other
        )
    [24] => Array
       (
            [19] => name
            [20] => address
            [21] => town
            [22] => state
            [23] => zip
            [24] => phone
            [25] => website
            [26] => other
            [27] => other
        )
)

但是数据返回如下:

Array
(
    [0] => 1 // first id in csv
    [1] => name
    [2] => address
    [3] => town
    [4] => state
    [5] => zip
    [6] => phone
    [7] => website
    [8] => other
    [9] => other
22 // id field
    [10] => name
    [11] => address
    [12] => town
    [13] => state
    [14] => zip
    [15] => phone
    [16] => website
    [17] => other
    [18] => other
24// id field
    [19] => name
    [20] => address
    [21] => town
    [22] => state
    [23] => zip
    [24] => phone
    [25] => website
    [26] => other
    [27] => other

关于如何修复此问题以使其看起来像顶部的数组有什么建议吗? 现在我正在做:

$csvfile = file_get_contents($targetFile);
$csv = str_getcsv($csvfile);

I am uploading a csv file and then parsing it using str_getcsv. All works great except that I need a way to cycle through them. Ideally, it'd be great to have the array come back and look like this:

Array (      
    [1] => Array
       (
            [0] => 1 // first id in csv
            [1] => name
            [2] => address
            [3] => town
            [4] => state
            [5] => zip
            [6] => phone
            [7] => website
            [8] => other
            [9] => other
        )
    [22] => Array
       (
            [10] => name
            [11] => address
            [12] => town
            [13] => state
            [14] => zip
            [15] => phone
            [16] => website
            [17] => other
            [18] => other
        )
    [24] => Array
       (
            [19] => name
            [20] => address
            [21] => town
            [22] => state
            [23] => zip
            [24] => phone
            [25] => website
            [26] => other
            [27] => other
        )
)

However the data comes back like the following:

Array
(
    [0] => 1 // first id in csv
    [1] => name
    [2] => address
    [3] => town
    [4] => state
    [5] => zip
    [6] => phone
    [7] => website
    [8] => other
    [9] => other
22 // id field
    [10] => name
    [11] => address
    [12] => town
    [13] => state
    [14] => zip
    [15] => phone
    [16] => website
    [17] => other
    [18] => other
24// id field
    [19] => name
    [20] => address
    [21] => town
    [22] => state
    [23] => zip
    [24] => phone
    [25] => website
    [26] => other
    [27] => other

Any suggestions on how to fix this to look like the array at the top?
right now I'm doing:

$csvfile = file_get_contents($targetFile);
$csv = str_getcsv($csvfile);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金橙橙 2024-12-22 14:54:29

str_getcsv() 期望作为参数传递的字符串为一条记录。
但由于您的源是一个文件,无论如何,最简单的方法可能是使用 fgetcsv() 而不是 str_getcsv()

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $data[] = fgetcsv($fp);
}
fclose($fp);

独立示例:

<?php
$targetFile = 'soTest.csv';
setup($targetFile);

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $data[] = fgetcsv($fp);
}
var_dump($data);


function setup($targetFile) {
    file_put_contents($targetFile, <<< eot
1,name,address,town,state,zip,phone,website,other,other
2,name,address,town,state,zip,phone,website,other,other
3,name,address,town,state,zip,phone,website,other,other
eot
    );
}

打印

array(3) {
  [0]=>
  array(10) {
    [0]=>
    string(1) "1"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
  [1]=>
  array(10) {
    [0]=>
    string(1) "2"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
  [2]=>
  array(10) {
    [0]=>
    string(1) "3"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
}

edit2: 使用每个记录的第一个元素作为结果数组中的键:

<?php
$targetFile = 'soTest.csv';
setup($targetFile);

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $row = fgetcsv($fp);
    $id = array_shift($row);
    $data[$id] = $row;
}
var_dump($data);


function setup($targetFile) {
    file_put_contents($targetFile, <<< eot
1,name,address,town,state,zip,phone,website,other,other
2,name,address,town,state,zip,phone,website,other,other
3,name,address,town,state,zip,phone,website,other,other
eot
    );
}

str_getcsv() expects the string passed as parameter to be one record.
But since your source is a file anyway the easiest way is probably to use fgetcsv() instead of str_getcsv()

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $data[] = fgetcsv($fp);
}
fclose($fp);

self-contained example:

<?php
$targetFile = 'soTest.csv';
setup($targetFile);

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $data[] = fgetcsv($fp);
}
var_dump($data);


function setup($targetFile) {
    file_put_contents($targetFile, <<< eot
1,name,address,town,state,zip,phone,website,other,other
2,name,address,town,state,zip,phone,website,other,other
3,name,address,town,state,zip,phone,website,other,other
eot
    );
}

prints

array(3) {
  [0]=>
  array(10) {
    [0]=>
    string(1) "1"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
  [1]=>
  array(10) {
    [0]=>
    string(1) "2"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
  [2]=>
  array(10) {
    [0]=>
    string(1) "3"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
}

edit2: For using the first element of each record as the key in the result array:

<?php
$targetFile = 'soTest.csv';
setup($targetFile);

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $row = fgetcsv($fp);
    $id = array_shift($row);
    $data[$id] = $row;
}
var_dump($data);


function setup($targetFile) {
    file_put_contents($targetFile, <<< eot
1,name,address,town,state,zip,phone,website,other,other
2,name,address,town,state,zip,phone,website,other,other
3,name,address,town,state,zip,phone,website,other,other
eot
    );
}
她说她爱他 2024-12-22 14:54:29

这不是这个世界上最美丽的事情,但我认为它有效。

//make the arrays needed
$csvtmp = array();
$csvFinal = array();

$csvfile = file_get_contents('test.csv');
$csv = str_getcsv($csvfile,"\n"); //make an array element for every record (new line)

//Loop through the result 
foreach ($csv as $key => $item) 
{
    array_push($csvtmp, str_getcsv($item,";")); //push each record to the csv temp array

    //here's the messy part. This set the key of the csvFinal array to the first field in the current record and for the value it gives it the array we got from the previous str_getcsv.
    $csvFinal[$csvtmp[$key][0]] = $csvtmp[$key]; 
    //Here we just unset the id row in the final array
    unset($csvFinal[$csvtmp[$key][0]][0]);

}

echo "<pre>";
    print_r($csvFinal);
echo "</pre>"; 

代码的结果:

Array
(
    [22] => Array
        (
            [1] => name1
            [2] => address1
            [3] => town1
            [4] => state1
            [5] => zip1
            [6] => phone1
            [7] => website1
            [8] => other1
            [9] => other1
        )

    [23] => Array
        (
            [1] => name2
            [2] => address2
            [3] => town2
            [4] => state2
            [5] => zip2
            [6] => phone2
            [7] => website2
            [8] => other1
            [9] => other1
        )

    [24] => Array
        (
            [1] => name3
            [2] => address3
            [3] => town3
            [4] => state3
            [5] => zip3
            [6] => phone3
            [7] => website3
            [8] => other1
            [9] => other1
        )
}

希望这有帮助

Not the pretties thing on this world but I think it works.

//make the arrays needed
$csvtmp = array();
$csvFinal = array();

$csvfile = file_get_contents('test.csv');
$csv = str_getcsv($csvfile,"\n"); //make an array element for every record (new line)

//Loop through the result 
foreach ($csv as $key => $item) 
{
    array_push($csvtmp, str_getcsv($item,";")); //push each record to the csv temp array

    //here's the messy part. This set the key of the csvFinal array to the first field in the current record and for the value it gives it the array we got from the previous str_getcsv.
    $csvFinal[$csvtmp[$key][0]] = $csvtmp[$key]; 
    //Here we just unset the id row in the final array
    unset($csvFinal[$csvtmp[$key][0]][0]);

}

echo "<pre>";
    print_r($csvFinal);
echo "</pre>"; 

result of the code:

Array
(
    [22] => Array
        (
            [1] => name1
            [2] => address1
            [3] => town1
            [4] => state1
            [5] => zip1
            [6] => phone1
            [7] => website1
            [8] => other1
            [9] => other1
        )

    [23] => Array
        (
            [1] => name2
            [2] => address2
            [3] => town2
            [4] => state2
            [5] => zip2
            [6] => phone2
            [7] => website2
            [8] => other1
            [9] => other1
        )

    [24] => Array
        (
            [1] => name3
            [2] => address3
            [3] => town3
            [4] => state3
            [5] => zip3
            [6] => phone3
            [7] => website3
            [8] => other1
            [9] => other1
        )
}

Hope this helps

朦胧时间 2024-12-22 14:54:29

好吧,只需计算一个条目的项目数,然后在一个循环中进行一个循环,

// loop on all items; $i+=$ITEM_SIZE go to the next item each outer loop
// (10 elements for ITEM in Opening Post's case)

// this for loops between all items
for ($i=0;$i<=$ARRAY_LENGHT;$i+=10)
{
    // this for loops between each item's elements
    for($c=1; $c<=10;$c++)
    {
        switch($c)
        {
            case 1:
              $id = $array[$i+$c];
              break;

            case 2:
              $name = $array[$i+$c];
              break;

            case 3:
              $address = $array[$i+$c];
              break;

            //etc to 10th element:

            case 10:
              $other = $array[$i+$c];
              break;

        }
    }

    // When the item is all done use the item
    // it as you need, example:

    echo "id: {$id}\nname: {$name}\naddress: {$adress}"; //etc

    // then unset($id,$name,$address, $etc);
    // for the next iteration of loop (next item)
}

这就是我刚刚所做的并且效果很好。

Well, just count the number of items an entry has then make a loop inside a loop

// loop on all items; $i+=$ITEM_SIZE go to the next item each outer loop
// (10 elements for ITEM in Opening Post's case)

// this for loops between all items
for ($i=0;$i<=$ARRAY_LENGHT;$i+=10)
{
    // this for loops between each item's elements
    for($c=1; $c<=10;$c++)
    {
        switch($c)
        {
            case 1:
              $id = $array[$i+$c];
              break;

            case 2:
              $name = $array[$i+$c];
              break;

            case 3:
              $address = $array[$i+$c];
              break;

            //etc to 10th element:

            case 10:
              $other = $array[$i+$c];
              break;

        }
    }

    // When the item is all done use the item
    // it as you need, example:

    echo "id: {$id}\nname: {$name}\naddress: {$adress}"; //etc

    // then unset($id,$name,$address, $etc);
    // for the next iteration of loop (next item)
}

That is what I just did and works very well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文