简体   繁体   English

使用多维PHP数组显示CSV文件中的数据

[英]Displaying data from a CSV file with multi-dimensional PHP array

I'm trying to pull data from a CSV file that contains vehicle make, model, mileage etc... 我正在尝试从包含车辆制造商,型号,行驶里程等的CSV文件中提取数据...

Using this example from php - 从php使用此示例-

<?php 
$csv = array_map('str_getcsv', file('csv/csvin.csv'));
array_walk($csv, function(&$a) use ($csv) {
    $a = array_combine($csv[0], $a);
});

array_shift($csv);

foreach($csv as $car){
    foreach($car as $key=>$value){
        echo "<div id='car'>".$key.":".$value."</div></br>";
    }
}
?>

This is the array I get - 这是我得到的数组-

BodyStyle:"Station Wagon"

"DaysInStock":"27"

"Make":"Toyota"

"Model":"Prius v"

"MSRP":"0"

"SellingPrice":"26995"

"StockNumber":"387515"

"Trim":"Three"

"VIN":"JTDZN3EU9E3306528"

"Year" :"2014"

Now when I attempt to manipulate it or pull any individual values I simply cannot. 现在,当我尝试操纵它或提取任何单个值时,我简直无法。 How would I go about displaying this information with HTML tags for each value? 如何为每个值显示带有HTML标记的信息?

I have tried this: 我已经试过了:

print_r ($csv[0]['Make']; 
echo $csv[0]['Make'];

Just to try and display a value but still nothing. 只是为了尝试显示一个值,但还是一无所获。 I noticed for some reason the "BodyStyle" doesn't contain quotes like the rest so something definitely seems fishy. 我注意到,由于某种原因,“ BodyStyle”不包含其余的引号,因此某些内容确实看起来有些混乱。

From here how would I strip the quotes and break out each value? 从这里我将如何去除引号并分解每个值?

This is the error being thrown - Invalid argument supplied for foreach() in 这是引发的错误-为中的foreach()提供了无效的参数

Thanks in advance! 提前致谢!

foreach($csv as $car){
    echo "<tr><td>Make:</td><td>".$car['Make']."</td></tr>";
}

alternatively: 或者:

foreach($csv as $car){
    foreach($car as $key=>$value){
        echo "<tr><td>".$key."</td><td>".$value."</td></tr>";
    }
}

alternatively: 或者:

echo $csv[0]['Make'];

Try this one : 试试这个:

print_r ($csv[0]['Make']);

Basically the $csv variable is an array, to get its values you have to use the loop function, something like : 基本上$ csv变量是一个数组,要获取其值,您必须使用loop函数,例如:

foreach ($csv as $data) {
    foreach ($data as $index => $value) {
        if ($index == "make") {
            echo $value;
        }
    }
}

If you notice it a bit, the outer array is called indexed array (array with indexes) and to traverse the values use foreach ($csv as $data), while the inner array is called associative array (this array does not use number as index, but a name), and to traverse it use foreach ($data as $index => $value). 如果您注意到了一点,则将外部数组称为索引数组(带有索引的数组),并使用foreach($ csv作为$ data)遍历值,而将内部数组称为关联数组(此数组不将数字用作索引,但有一个名称),并使用foreach($ data作为$ index => $ value)进行遍历。 Try it and you'll see :) PS: Sorry I didn't notice the inner array, for this case Richard's answer is the correct one. 尝试一下,您会看到:) PS:抱歉,我没有注意到内部数组,在这种情况下,Richard的答案是正确的。 I have added a credit to his answer for giving a correct answer. 我为他的回答提供了功劳,因为他给出了正确的答案。

So I figured out that I only need 1 foreach loop like so - 所以我发现我只需要1个foreach循环,就像这样-

foreach($csv as $car){
    $type = $car[0];
    $echo $type;
}

Works now and thanks all for the help! 现在可以工作,感谢大家的帮助!

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

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