使用foreach循环更改多个字段中的嵌套数组值

qcuzuvrc  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(369)

尝试循环遍历数组(mysql db表的查询结果),找到四个不同的字段并添加前缀。
我有四个图像版本——full size、med、small和thumb——作为表字段(mysql)。这些值是文件名。我要加前缀https://... 这样我就有了每张照片的完整地址。
我的查询返回一个关联数组( $myArray ). foreach循环查找 $key 匹配到图像字段,如果为true,则显示当前值,然后添加前缀,然后显示新值。显示为“新值”的内容包括前缀,因此部分代码按预期工作。
但是,当我显示 $myArray 之后,“更改的”值不会更改。它们只是没有前缀的原始文件名。
我不知道如何将新值应用于数组。
这是我的密码:

$prefix = 'example.com/assets/images/';

foreach($myArray as $key => $value) {

  if ($key == 'image_full_size') {
     echo 'Current value: ' . $value['image_full_size'] . '<br>';
     $value['image_full_size'] = $prefix . $value['image_full_size'];
     echo 'New value: ' . $value['image_full_size'] . '<br>';
  }

  if ($key == 'image_med') {
     echo 'Current value: ' . $value['image_med'] . '<br>';
     $value['image_med'] = $prefix . $value['image_med'];
     echo 'New value: ' . $value['image_med']. '<br>';
  }

  if ($key == 'image_small') {
     echo 'Current value: ' . $value['image_small'] . '<br>';
     $value['image_small'] = $prefix . $value['image_small'];
     echo 'New value: ' . $value['image_small']. '<br>';
  }

  if ($key == 'image_thumb') {
     echo 'Current value: ' . $value['image_thumb'] . '<br>';
     $value['image_thumb'] = $prefix . $value['image_thumb'];
     echo 'New value: ' . $value['image_thumb']. '<br>';
  }
}

如果能帮我找出错误,我将不胜感激。
数组中的前两条记录:

Array
(
   [0] => Array
       (
           [id] => 1
           [laser_series] => GTO FLX
           [laser_model] => GTO/FLX01
           [laser_color] => red
           [price] => 118.75
           [image_full_size] => example.com/assets/images/laser-pistol/gto-flx01_black_red_1080x720.png
           [image_med] => example.com/assets/images/laser-pistol/gto-flx01_red_med.png
           [image_small] => example.com/assets/images/laser-pistol/gto-flx01_red_small.png
           [image_thumb] => example.com/assets/images/laser-pistol/gto-flx01_red_thumb.png
           [ad_blurb] => Custom-designed to fit this brand and model. The GTO/FLX series laser is the most technologically-advanced laser sight available for this popular pistol.
           [feature01] => Grip-Touch Activation by using FLX
           [feature02] => Side-Touch Activation without FLX
           [feature03] => Ultra-bright 635nm red laser
           [special_message] => 
           [mfr_name] => Kel-Tec
       )

   [1] => Array
       (
           [id] => 3
           [laser_series] => GTO FLX
           [laser_model] => GTO/FLX02
           [laser_color] => red
           [price] => 118.75
           [image_full_size] => gto-flx02_black_red_1080x720.png
           [image_med] => gto-flx02_red_med.png
           [image_small] => gto-flx02_red_small.png
           [image_thumb] => gto-flx02_red_thumb.png
           [ad_blurb] => Custom-designed to fit this brand and model. The GTO/FLX series laser is the most technologically-advanced laser sight available for this popular pistol.
           [feature01] => Grip-Touch Activation by using FLX
           [feature02] => Side-Touch Activation without FLX
           [feature03] => Ultra-bright 635nm red laser
           [special_message] => 
           [mfr_name] => Kel-Tec
       )
)
ibrsph3r

ibrsph3r1#

foreach循环查找与image字段匹配的$key,如果为true,则显示当前值,然后添加前缀,然后显示新值。显示为“新值”的内容包括前缀,因此部分代码按预期工作。
不-它没有按预期工作。看起来就是这样。
行键是数字。但你把它们比作这样的字符串: $key == 'image_full_size' . 在本例中,php似乎将字符串转换为一个数字,这就是 0 . 所以这个条件只有在 $key0 . 你可以在这里看到这种行为:http://rextester.com/xdorw8182
这就是为什么它只适用于键所在的第一行 0 .
所以你的尝试是错误的。一个可行的解决方案并没有那么复杂:

foreach($myArray as $key => $row) {
    $myArray[$key]['image_full_size'] = $prefix . $row['image_full_size'];
    $myArray[$key]['image_med']       = $prefix . $row['image_med'];
    $myArray[$key]['image_small']     = $prefix . $row['image_small'];
    $myArray[$key]['image_thumb']     = $prefix . $row['image_thumb'];
}

演示:http://rextester.com/jea91624

原始答案:

在里面

foreach($myArray as $key => $value) {
     $value['image_full_size'] = $prefix . $value['image_full_size'];
}
``` `$value` 是的(本地)副本 `$myArray[$key]` 而你正在改变那份拷贝。如果要更改中的原始行 `$myArray` 你应该通过 `$key` :

$myArray[$key]['image_full_size'] = $prefix . $value['image_full_size'];

另一种方法是通过引用

foreach($myArray as $key => &$value) {...}

但这可能会有一些不必要的副作用,所以你最好避免。

相关问题