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

时间:2017-01-24 13:12:14

标签: php arrays variables multidimensional-array

PHP的新手。我有一个csv feed,我试图在正确的位置显示每一行。将feed加载到一个多维数组中,我可以回显出这样的特定位置。

<div class="icons">
  <div class="first">
  </div>
</div>

<div class="outer-box">
</div>

但我尝试在执行while循环时使用变量$ row的值而不是第一个数字。

类似的东西:

echo $readcsv[0][2]

我已经尝试过next(),str_replace()和strtr()但是在循环运行时它们似乎都没有工作。

echo $readcsv[$row][2]

CSV文件:

$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);
}

1 个答案:

答案 0 :(得分:0)

我不太确定你的代码和问题究竟是你要做什么,但也许是这样的事情

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);
}