我正在尝试删除从ftp_nlist输出的两个数组字符串的两部分。我正在与CI合作。另请注意,视图中的多选列表正在进行,并且不是正确的代码。但是,我认为我应该披露显示完整视图文件的其他选择列表。
{ ["parent_directory_one/child_directory_one"]=> int(0) ["parent_directory_one/child_directory_two"]=> int(1) }
如何将以上内容更改为:
{ ["child_directory_one"]=> int(0) ["child_directory_two"]=> int(1) }
然后如何更改如下:
{ ["parent_directory_one/child_directory_one/child_files_one"]=> int(0) ["parent_directory_two/child_directory_two/child_files_two"]=> int(1) }
到此:
{ ["child_files_one"]=> int(0) ["child_files_two"]=> int(1) }
//controller//
这里有一些我尝试过的例子和视图文件正在构建中:
//read default remote
$glue = '/'; $strs = ''; $root_piece_rm = ltrim($strs, './');
$list_rm = ftp_nlist($conn_id,$root_piece_rm);
$list_rm = array_flip($list_rm);
$data['list_rm'] = $list_rm;
//end read default remote
if($rm_targets):
$glue = '/'; $list_sub_rm = ftp_nlist($conn_id,$rm_targets);
foreach($list_sub_rm as $keys):
$list_sub_rm = ltrim($keys,$rm_targets.$glue);
$data['list_sub_rm'] = var_dump(array($list_sub_rm));
endforeach;
endif;
但子目录的以下代码是问题,我认为ltrim不会起作用。然而,var_dump正在输出根据$ rm_targets。$ glue模式修剪的字符串,需要删除只显示子目录。
//CONTROLLER//
$rm_targets = $this->input->post('pt_rm_dirs',TRUE);
if($rm_targets):
$glue = '/'; $list_sub_rm = ftp_nlist($conn_id,$rm_targets);
$list_sub_rm = array_flip($list_sub_rm);
$list_sub_rm = array_keys($list_sub_rm);
foreach($list_sub_rm as $index):
$eraser = ltrim($index,$rm_targets.$glue);
endforeach;
$data['list_sub_rm'] = $list_sub_rm;
endif;
基本上我需要从数组的以下字符串中删除父目录{[“jm_gallery / new_test_origs”] => int(0)[“jm_gallery / test_thumbs”] => int(1)}
我不确定目前需要如何设置视图或控制器。
我从下面的var_dump得到的输出是我需要在多选列表中显示的内容。它本质上是子目录,但正斜杠和父删除。在我开始使用ftp_nlist之前,我对这种情况没有遇到任何困难,而且非常令人不安。
//CONTROLLER//
if($rm_targets):
$glue = '/'; $list_sub_rm = ftp_nlist($conn_id,$rm_targets);
foreach($list_sub_rm as $keys):
$list_sub_rm = ltrim($keys,$rm_targets.$glue);
$data['list_sub_rm'] = var_dump(array($list_sub_rm));
endforeach;
endif;
这里是正在构建的视图文件,我想再次注意,所以循环还不正确:
//VIEW//
<?php if(!defined('BASEPATH'))exit('No direct script access allowed'); ?>
<?php echo form_open('test_sftp/test_function')?>
<select name="pt_rm_dirs" multiple size="10">
<option value="select one"<?php echo set_select('pt_rm_dirs', 'select_one');?> >select one</option>
<?php foreach($list_rm as $folder_rm_dirs => $contents_rm_dirs):?>
<option value="<?php echo $folder_rm_dirs?>"<?php echo set_select('pt_rm_dirs', '$folder_rm_dirs');?> ><?php echo $folder_rm_dirs?></option>
<?php endforeach;?>
<?php unset($contents_rm_dirs);?>
第2步:
为所选的远程目录指定新名称:
<select name="ct_rm_subdirs" multiple size="10">
<option value="select one"<?php echo set_select('ct_rm_subdirs', 'select_one');?> >select one</option>
<?php foreach($list_sub_rm as $sub_folder_rm):?>
<option value="<?php echo $sub_folder_rm?>"<?php echo set_select('ct_rm_subdirs', '$sub_folder_rm');?> ><?php echo $sub_folder_rm?></option>
<?php endforeach;?>
<?php unset($sub_contents_rm);?>
</select>
<select name="rm_sub_dirs_files" multiple size="15">
<option value="select one"<?php echo set_select('rm_sub_dirs_files', 'select_one');?> >select one</option>
<?php foreach($list_sub_rm as $sub_folder_rm => $sub_contents_rm):?>
<optgroup label="<?php echo $sub_folder_rm?>">
<?php foreach($sub_contents_rm as $files):?>
<option value="<?php echo $files?>"<?php echo set_select('rm_sub_dirs_files', '$files');?> ><?php echo $files?></option>
<?php endforeach;?>
<?php endforeach;?>
<?php unset($sub_contents_rm);?>
<?php unset($files);?>
</optgroup>
</select>
<?php echo form_submit('submit', 'Submit');?>
<?php echo form_close()?>
</body>
</html>
<?php echo form_close();?>
更新:当我var_dump下面的代码时,字符串会因为我需要而改变,但为什么我不能通过View回显它们?我尝试将字符串转换为一个有问题的数组。
if($rm_targets):
$glue = '/';
$list_sub_rm = ftp_nlist($conn_id,$rm_targets);
foreach($list_sub_rm as $keys):
$list_sub_rm = ltrim($keys,$rm_targets.$glue);
$data['list_sub_rm'] = var_dump($list_sub_rm);
endforeach;
endif;
任何帮助非常感谢!!! 感谢!!!
答案 0 :(得分:0)
看起来文件名是数组中的键,这让我感激不尽。
foreach ( $list_sub_rm as $key => $value ) {
$key = preg_replace("#.*/([^/]*)$#", "$1", $key);
$newArray[$key] = $value;
}