使用foreach循环更改数组中的文件名

时间:2017-10-26 13:40:00

标签: php arrays

我有这个数组

Array(
[0] => Array
    (
        [value] => /4/9/492.jpg
    )

[1] => Array
    (
        [value] => /4/9/493.jpg
    )

[2] => Array
    (
        [value] => /4/9/494.jpg
    )

[3] => Array
    (
        [value] => /4/9/495.jpg
    )

[4] => Array
    (
        [value] => /w/i/witbox_schwarz.jpg
    )

)

我想在此方案后使用新文件名更改值。

$n = count($arr);
foreach ($arr as $key => $value) {
  for ($i = 0; $i < $n; $i++) {
    if ($key == $i) {
      echo 'old filename is ' . $value . ' ... new filename will be ' . $prod_result['products_id'] . '_' . $key . '.' . pathinfo($value, PATHINFO_EXTENSION); 
    }
  }
}

一切都很好,但我不能在foreach循环中从数组中获取旧值。

1 个答案:

答案 0 :(得分:1)

正如我在我的系统中所做的那样完美无缺

$arr = [0 => ['value' => "/4/9/492.jpg"],1 => ['value' => "/4/9/493.jpg"],2 => ['value' => "/4/9/494.jpg"],3 => ['value' => "/4/9/495.jpg"],4 => ['value' => "/w/i/witbox_schwarz.jpg"]];


$n = count($arr);
foreach ($arr as $key => $value) {
  for ($i = 0; $i < $n; $i++) {
    if ($key == $i) {
      echo 'old filename is ' . $value['value'] . ' ... new filename will be ' . $prod_result['products_id'] . '_' . $key . '.' . pathinfo($value['value'], PATHINFO_EXTENSION); 
    }
  }
}