gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:02:15 26 4
gpt4 key购买 nike

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

我有四个图像版本——全尺寸、中型、小尺寸、拇指——作为表字段(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
)
)

最佳答案

The foreach loop looks for a $key match to the image field, and iftrue displays the current value, then adds the prefix, then displaysthe new value. What displays as the "New value" includes the prefix,so that part of the code is working as expected.

否 - 它没有按预期工作。看起来就是这样。

您的行键是数字。但是您将它们与这样的字符串进行比较:$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) {...}

但这可能会产生一些不需要的副作用,所以你最好避免这种情况。

关于php - 使用 foreach 循环更改多个字段中的嵌套数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48241068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com