在CodeIgnitor中,我使用directory_map得到这个数组。
Array
(
[0] => preview.zip
[1] => RealEsta - Email Marketing Template.zip
[2] => thumbnail
[3] => thumbnail.jpg
)
我想从数组中删除目录名称。
Array
(
[0] => preview.zip
[1] => RealEsta - Email Marketing Template.zip
[2] => thumbnail.jpg
)
喜欢这个。我将如何从此文件列表数组中删除此目录。
根据 CodeIgniter 支持页面,目录名称不会作为数组值列出,而是作为数组索引列出。
https://ellislab.com/codeigniter/user-guide/helpers/directory_helper.html
您可以使用
is_dir()
例:
<?php
// Your array
$files_and_dirs = array(
'preview.zip',
'RealEsta - Email Marketing Template.zip',
'thumbnail',
'thumbnail.jpg'
);
// Now loop the above array
foreach($files_and_dirs as $key => $value)
{
if(is_dir("abc/xyz/".$value))
{
unset($files_and_dirs[$key]);
}
}
你可以用多种方式解决它(目前我可以想到 3 种方式)。
方式1:
$map = directory_map('./uploads/'.$user_id.'/files', 2, FALSE);//set second parameter as 2
foreach($map as $key=>$m)
{
if(is_array($m))
{
unset($map[$key]);
}
}
print_r($map);//now $map only contains files name
方式2:
$map = directory_map('./uploads/'.$user_id.'/files', 2, FALSE);//set second parameter as 2
foreach($map as $key=>$m)
{
if(!is_int($key))
{
unset($map[$key]);
}
}
print_r($map);
方式3:
$map = directory_map('./uploads/'.$user_id.'/files', 1, FALSE);//As you did
foreach($map as $key=>$m)
{
if(is_dir('./uploads/'.$user_id.'/files'.$m))
{
unset($map[$key]);
}
}
print_r($map);