PHP使用数组值重命名

时间:2018-01-17 21:04:13

标签: php

我搜索过这个交易所和谷歌我遇到的问题。没有结果。所以我再次转向你。

情况就是这样:我在数组中有大量的文件名,包括它们各自的路径。这个数组是动态生成的。

我需要重命名此数组中的文件。

我虽然我将重命名放在foreach中以遍历每个文件。

foreach($files as $value) {

rename($value,"downloads/gmp-guidelines/pics/gmp-guideline-pics-".$LFocus."-".$Title);

}

我从PHP收到以下错误:

Warning: rename() expects parameter 1 to be a valid path, array given in /home/users/whatever/whatever/test.php on line 58

事实上当我在$ value上执行var_dump时,我得到以下内容:

array(1) { [0]=> string(95) "downloads/gmp-guidelines/pics/pi_038_1_aide_memoire_on_assessment_of_qrm_implementation (1).pdf" }

事情是,我无法弄清楚如何从包含字符串的数组中获取$ value到字符串。

我希望你们能帮助我!

var_export($ files):

array ( 0 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_038_1_aide_memoire_on_assessment_of_qrm_implementation (1).pdf', ), 1 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_021_2_aide_memoire_on_gmp_gcp (1).pdf', ), 2 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_024_2_aide_memoires_on_biotech (2).pdf', ), 3 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_023_2_inspection_of_quality_control_laboratories (2).pdf', ), 4 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_009_3_aide_memoire_on_utilities (1).pdf', ), 5 => array ( ), 6 => array ( ), 7 => array ( ), 8 => array ( ), 9 => array ( ), 10 => array ( 0 => 'downloads/gmp-guidelines/pics/pe_008_4_site_master_file_copy1.pdf', ), 11 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_011_3_recommendation_on_computerised_systems (5).pdf', ), 12 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_005_3_parametric_release.pdf', ), 13 => array ( 0 => 'downloads/gmp-guidelines/pics/pe_010_4_guide_to_good_practices_for_the_preparation_of_medicinal_products_in_healthcare_establishments_copy1.pdf', ), 14 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_014_3_recommendation_on_isolators.pdf', ), 15 => array ( 0 => 'downloads/gmp-guidelines/pics/ps_w_01_2015_joint_pics_ema_concept_paper_on_the_revision_of_annex_1.pdf', ), 16 => array ( ), 17 => array ( ), 18 => array ( ), 19 => array ( ), 20 => array ( ), 21 => array ( 0 => 'downloads/gmp-guidelines/pics/pe_005_3_pics_gmp_guide_for_blood_establishments.pdf', ), 22 => array ( 0 => 'downloads/gmp-guidelines/pics/pe_011_1_pics_gdp_guide_copy1 (1).pdf', ), 23 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_008_3_guide_to_plasma_inspections.pdf', ), 24 => array ( ), 25 => array ( 0 => 'downloads/gmp-guidelines/pics/ps_inf_20_2011_qa_distribution_activities_for_apis_may_2010.pdf', ), 26 => array ( ), 27 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_012_3_recommendation_on_sterility_testing.pdf', ), 28 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_020_3_smf_plasma_warehouses_copy1.doc', 1 => 'downloads/gmp-guidelines/pics/pi_020_3_smf_plasma_warehouses_copy1.pdf', ), 29 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_019_3_smf_source_plasma_establishments.doc', 1 => 'downloads/gmp-guidelines/pics/pi_019_3_smf_source_plasma_establishments.pdf', ), 30 => array ( ), 31 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_006_3_recommendation_on_validation_master_plan.pdf', ), 32 => array ( ), )

1 个答案:

答案 0 :(得分:3)

您有一个数组数组,因此$value是一个数组。相应地访问它:

<击>

<击>
foreach($files as $value) {
    rename($value[0],"downloads/gmp-guidelines/pics/gmp-guideline-pics-".$LFocus."-".$Title);
}

<击>

使用您提供的var_export(),看起来您也循环遍历值(也可以是数组)。请注意:

[28] => Array
    (
        [0] => downloads/gmp-guidelines/pics/pi_020_3_smf_plasma_warehouses_copy1.doc
        [1] => downloads/gmp-guidelines/pics/pi_020_3_smf_plasma_warehouses_copy1.pdf
    )

数组索引28中有2个文件,所以我们需要2个foreach循环。如果数组为空,则内部foreach循环将不执行任何操作。 (见this playground):

<?php
$files = array ( 
    0 => array (
        0 => 'downloads/gmp-guidelines/pics/pi_038_1_aide_memoire_on_asse‌​ssment_of_qrm_implem‌​entation (1).pdf', 
    ),
    1 => array ( ),
    2 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_021_2_aide_memoire_on_gmp_‌​gcp (1).pdf', )
);

foreach( $files as $values ) {
    //You may have multiple values, so we need to loop through them.
    foreach ( $values as $value ) {
      rename($value, "downloads/gmp-guidelines/pics/gmp-guideline-pics-".$LFocus."-".$Title);
    }
}