PHP CSV到具有行标题的关联数组 [英] PHP CSV to Associative Array with Row Headings

查看:66
本文介绍了PHP CSV到具有行标题的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件,其中的列和行中的标题都是我作为关联数组所需要的,而我在SO上尝试过的示例对我来说没有用.

I have a CSV file with headings in both the columns and rows that i need as an associative array and the examples i have tried on SO haven't worked for me.

我找到的最接近的示例是 CSV到关联数组,但是我无法打印数据,所以我一定会丢失一些东西.

The closest example i found was CSV to Associative Array but i couldn't print the data, so i must be missing something.

任何帮助将不胜感激

$all_rows = array();

$header = 1;//null; // has header
if (($handle = fopen($csv_file, "r")) !== FALSE) {
    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if ($header === null) {
            $header = $row;
            continue;
        }
        $all_rows[] = array_combine($header, $row);
    }

    fclose($handle);
}


// CSV Example
// [FOOD][IN_Stock][On_Order]
// [Apples][1302][2304]
// [Oranges][4356][335]


foreach ($all_rows as $key => $value) {

    // This prints ok based on the row number
    echo $all_rows[$key]['Instock'];

    // But i need to be able to call data based on the Food Type
    echo $all_rows['Apples']['In_Stock'];
    echo $all_rows['Apples']['On_order'];
    echo $all_rows['Oranges']['In_Stock'];
    echo $all_rows['Oranges']['On_order'];

}

我可以通过调用行号来打印数据,但是数据并不总是相同的,因此需要基于第一列中的标题进行打印.

I can print the data by calling the row number, but the data will not always be the same and therefore need to print based on the headers in the first column.

按要求添加打印的数组:

Added the printed Array as Requested:

Array
(
[0] => Array
    (
        [Food] => Apples
        [In_Stock] => 6329
        [On_Order] => 375
    )

[1] => Array
    (
        [Food] => Pears
        [In_Stock] => 3329
        [On_Order] => 275
    )

[2] => Array
    (
        [Food] => Oranges
        [In_Stock] => 629
        [On_Order] => 170
    )
  ) 

让其他人需要它,这就是我所使用的:

Encase anyone else needs it, this is what i used:

$csv = array();
foreach ($all_rows as $row) {
if($row) {
    foreach($row as $key => $val) {

        $csv[$val]['In_Stock'] = $row["In_Stock"];
        $csv[$val]['On_order'] = $row["On_order"];

    }
}
}

推荐答案

您需要第二个foreach来实现所需的功能.

You need a second foreach to achieve what you want.

类似这样的东西:

foreach ($all_rows as $row) {
    if($row) {
        foreach($row as $key => $val) {
            if($key == "Food" && $val == "Apples") {
                echo "Apples: " . "<br>";
                echo $row["In_Stock"] . "<br>";
                echo $row["On_order"] . "<br>";
            }
            else if($key == "Food" && $val == "Oranges") {
                echo "Oranges: " . "<br>";
                echo $row["In_Stock"] . "<br>";
                echo $row["On_order"] . "<br>";
            }
        }
    }
}

如果您只想在页面上打印值,可以这样做.

If you just want to print the values on your page, this could do it.

但是我既然您提到过仅根据食物类型来调用值,您可以在此基础上创建一个函数.

But I since you've mentioned to just call the values based on food type, you can create a function out of this.

这篇关于PHP CSV到具有行标题的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆