mysql一个循环匹配多个值_使用foreach循环更改多个字段中的嵌套数组值

尝试循环遍历数组(mysql db table的查询结果),找到四个不同的字段并添加前缀。

我有四个图像版本 - 全尺寸,中等,小,拇指 - 作为表字段(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'] . '
';

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

echo 'New value: ' . $value['image_full_size'] . '
';

}

if ($key == 'image_med') {

echo 'Current value: ' . $value['image_med'] . '
';

$value['image_med'] = $prefix . $value['image_med'];

echo 'New value: ' . $value['image_med']. '
';

}

if ($key == 'image_small') {

echo 'Current value: ' . $value['image_small'] . '
';

$value['image_small'] = $prefix . $value['image_small'];

echo 'New value: ' . $value['image_small']. '
';

}

if ($key == 'image_thumb') {

echo 'Current value: ' . $value['image_thumb'] . '
';

$value['image_thumb'] = $prefix . $value['image_thumb'];

echo 'New value: ' . $value['image_thumb']. '
';

}

}

我很感激找到我的错误的任何帮助。

数组中的前两条记录:

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

)

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值