简体   繁体   English

PHP将multidimentional数组的索引更改为变量的值

[英]PHP changing the index of multidimentional array to value of variable

Pretty new to PHP. PHP的新手。 I have a csv feed that i am trying to display every line of in the right place. 我有一个csv feed,我试图在正确的位置显示每一行。 The feed is loaded into a multidimentional array and I can echo out specific places like this. 将feed加载到一个多维数组中,我可以回显出这样的特定位置。

echo $readcsv[0][2]

But I am trying to use the value of the variable $row in stead of the first number while doing a while loop. 但我尝试使用变量$ row的值而不是第一个数字,同时执行while循环。

Something like: 就像是:

echo $readcsv[$row][2]

I have tried next(), str_replace() and strtr() but none of them seems to work while the loop is running. 我已经尝试了next(),str_replace()和strtr()但是在循环运行时它们似乎都没有工作。

$row = 1;
$readcsv = array(); 

if (($handle = fopen("file.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $num = count($data);

        echo $readcsv[$row][2];

        $row++;
        for ($c=0; $c < $num; $c++) {
        }
        $mycsvfile[] = $data; 
    }
    fclose($handle);
}

CSV file: CSV文件:

title;image_link;link;empty;price;
1;back.gif;http://link.com;;19.95;
2;back.gif;http://link.com;;19.95;
3;back.gif;http://link.com;;19.95;
4;back.gif;http://link.com;;19.95;
5;back.gif;http://link.com;;19.95;

I am not quite sure from your code and question exactly what you are trying to do, but maybe its something like this 我不太确定你的代码和问题到底你要做什么,但也许是这样的

if (($handle = fopen("file.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {

        // you load the line into $data and its a one dimentional array
        // occurance 2 is the `link` (http://link.com) if that what you want to echo
        echo $data[2];

        // as you have a loop, maybe you were trying to
        // echo each fields from the csv so you can do that like this
        foreach ($data as $idx => $value) {

            echo "Column $idx = $value";
        }
        $mycsvfile[] = $data;
    }
    fclose($handle);
}

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

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