PHP - 显示数组值

时间:2013-12-12 11:24:02

标签: php arrays

我有一个小问题如何显示此脚本的数组值。 它在文件名后用dir中的字符串“tn”进行扫描并给出一个数组。

array(2) { [0]=> string(13) "pics/tn_2.jpg" [1]=> string(13) "pics/tn_1.jpg" }

我需要知道如何只显示一个值,例如echo array [0]

这就是整个代码:

<?php

 error_reporting(E_ALL);

 $searchDir = 'pics/';
 $searchExtList = array('.jpg');
 $searchString = 'tn';

 $allFiles = everythingFrom($searchDir,$searchExtList,$searchString);

 var_dump($allFiles);

function everythingFrom($baseDir,$extList,$searchStr) {
 $ob = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir),     RecursiveIteratorIterator::SELF_FIRST);
foreach($ob as $name => $object){
    if (is_file($name)) {
        foreach($extList as $k => $ext) {
            if (substr($name,(strlen($ext) * -1)) == $ext) {
                $tmp = file_get_contents($name);
                if (strpos($tmp,$searchStr) !== false) {
                    $files[] = $name;
                }
            }
        }
    }
}
return $files;

}

echo $files['0']; // doesnt works !!!!!!!!!!!
  ?>

3 个答案:

答案 0 :(得分:4)

$allFiles = everythingFrom($searchDir,$searchExtList,$searchString);
echo $allFile[0];

答案 1 :(得分:1)

$allFiles = everythingFrom($searchDir,$searchExtList,$searchString);

现在你的$ allFiles数组看起来像这样:

{ [0]=> string(13) "pics/tn_2.jpg" [1]=> string(13) "pics/tn_1.jpg" }

从[0]获取数据写下:

echo $allFiles[0];

答案 2 :(得分:0)

您应该首先将$ files声明为数组。例如:

function everythingFrom($baseDir,$extList,$searchStr) {
    $files = array();
    ....