创建多维数组并访问字段

时间:2021-11-07 15:55:26

I have been running around in circles over this for days ... please help.

我已经在这周围跑了几天......请帮帮忙。

I am trying to create a multidimensional array based on the number of files in a directory and the parsed filename ...

我试图根据目录中的文件数和解析的文件名创建一个多维数组...

foreach ($files as $file) {
     echo "$file[0] $file[1]  <br>\n" ;  #file[0]=Unix timestamp; file[1]=filename
     $pn = explode('.', $file[1]);
     $ndt = explode('_',array_shift($pn)) ;
         foreach ($ndt as $arndt) {
         $items[$arndt] = $ndt ; //this part does not work
         echo "$ndt[0] $ndt[1] $ndt[2] $ndt[3] $ndt[4]" ;
     }
    print_r($items[$arndt]) ;

}

The output of my array is this:

我的数组的输出是这样的:

Array ( [0] => OLPH [1] => Barbecue [2] => 03132013 [3] => 11am [4] => 2pm )

数组([0] => OLPH [1] =>烧烤[2] => 03132013 [3] =>上午11点[4] =>下午2点)

Note: I just have 1 file in the directory for testing purposes, but there will be more, hence the need for a multidimensional array ...

注意:我在目录中只有1个文件用于测试目的,但会有更多,因此需要一个多维数组......

I then try to access the array in my html using this:

然后我尝试使用以下方法访问我的html中的数组:

<h4><a href="#"><?php echo "$items[$arndt]. $ndt[1]" ?></a></h4>

.... naturally, this output does not print the results that I want .... For every file[1] I want to be able to print $arndt[] and access it using $items[][] notation.... however it just prints Array[]Array[] .... Please help ?

....当然,这个输出不打印我想要的结果....对于每个文件[1]我希望能够打印$ arndt []并使用$ items [] []表示法访问它。 ..然而它只是打印Array [] Array [] ....请帮帮忙?

Thanks in advance,

提前致谢,

Carlos

1 个解决方案

#1


0  

echoing/printing an array in string context just gives you Array. If you're dealing with multi-dimensional arrays, each dimension has to have its own loop to print out its contents.

在字符串上下文中回显/打印数组只是为了给出数组。如果您正在处理多维数组,则每个维度都必须有自己的循环来打印其内容。

e.g.

$arr1d = array('foo' => 'bar'); // 1D array
echo $arr1d; // outputs "Array"

$arr2d = array('foo' => array('bar' => 'baz')); // 2D array
echo $arr2d; // outputs 'Array';
echo $arr2d['foo']; // outputs 'Array'
echo $arr2d['foo']['bar']; // outputs 'baz'

foreach($arr2d as $key1 => $val1) {
    echo $val1; // outputs 'Array';
    foreach($val1 as $key2 => $val2) {
        echo $val; // outputs 'Baz'
    }
}

#1


0  

echoing/printing an array in string context just gives you Array. If you're dealing with multi-dimensional arrays, each dimension has to have its own loop to print out its contents.

在字符串上下文中回显/打印数组只是为了给出数组。如果您正在处理多维数组,则每个维度都必须有自己的循环来打印其内容。

e.g.

$arr1d = array('foo' => 'bar'); // 1D array
echo $arr1d; // outputs "Array"

$arr2d = array('foo' => array('bar' => 'baz')); // 2D array
echo $arr2d; // outputs 'Array';
echo $arr2d['foo']; // outputs 'Array'
echo $arr2d['foo']['bar']; // outputs 'baz'

foreach($arr2d as $key1 => $val1) {
    echo $val1; // outputs 'Array';
    foreach($val1 as $key2 => $val2) {
        echo $val; // outputs 'Baz'
    }
}