簡體   English   中英

使用foreach循環在多個字段中更改嵌套數組的值

[英]Change nested array values in multiple fields using foreach loop

嘗試循環遍歷數組(mysql db表的查詢結果),找到四個不同的字段並添加前綴。

我有四個圖像版本-全尺寸,中型,小型,拇指形-作為表格字段(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
       )
)

foreach循環查找與圖像字段匹配的$ 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) {...}

但這可能會帶來一些不良的副作用,因此最好避免這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM