You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将$defaultValues嵌套数组中colors和lessons替换为$replace多值

PHP多值替换输出问题修复

问题说明

现有PHP代码从$defaultValues数组输出内容,其中colors、lessons为嵌套数组。代码通过str_replace从$search数组提取子串并用$replace数组的值替换,但当前仅能输出单个值。需要将colors替换为["4","5","6"]的逗号分隔形式,lessons替换为["7","8","9"]的逗号分隔形式,输出如Your favorite color: 4,5,6的格式。

当前输出

Name: 1
Age: 2
Whe are u from: 3
Your favorite color: 4
Choose courses: 7
Your comment: 10

期望输出

Name: 1
Age: 2
Whe are u from: 3
Your favorite color: 4,5,6
Choose courses: 7,8,9
Your comment: 10

现有代码

$defaultValues = [
  "name"     =>  "Name:                        write your name                     ", 
  "age"      =>  "Age:                          enter your age                      ",
  "from"     =>  "Whe are u from:              write where are you from            ", 
  "colors"   =>  ["Your favorite color:      you didn't say your favorite color  "],        
  "lessons"  =>  ["Choose courses:          you have not chosen any course      "],
  "comment"  =>  "Your comment:             no comments                         ",     
];
 
$search = [
  "name"     =>  "write your name", 
  "age"      =>  "enter your age", 
  "from"     =>  "write where are you from", 
  "colors"   =>  ["you didn't say your favorite color"],        
  "lessons"  =>  ["you have not chosen any course"],
  "comment"  =>  "no comments",   
];
 
$replace = [
  "name"     =>  "1", 
  "age"      =>  "2",
  "from"     =>  "3",
  "colors"   =>  ["4", "5", "6"],        
  "lessons"  =>  ["7", "8", "9"],
  "comment"  =>  "10",       
];
 
 
foreach($defaultValues as $key => $items){
  echo "";
 
  if(is_array($items)){
    foreach($items as $child){
        $items = str_replace($search[$key], $replace[$key], $child);
    }
  }
 
  echo $items = str_replace($search[$key], $replace[$key], $items);   
 
  echo "";  
}

修复后的代码

$defaultValues = [
  "name"     =>  "Name:                        write your name                     ", 
  "age"      =>  "Age:                          enter your age                      ",
  "from"     =>  "Whe are u from:              write where are you from            ", 
  "colors"   =>  ["Your favorite color:      you didn't say your favorite color  "],        
  "lessons"  =>  ["Choose courses:          you have not chosen any course      "],
  "comment"  =>  "Your comment:             no comments                         ",     
];
 
$search = [
  "name"     =>  "write your name", 
  "age"      =>  "enter your age", 
  "from"     =>  "write where are you from", 
  "colors"   =>  ["you didn't say your favorite color"],        
  "lessons"  =>  ["you have not chosen any course"],
  "comment"  =>  "no comments",   
];
 
$replace = [
  "name"     =>  "1", 
  "age"      =>  "2",
  "from"     =>  "3",
  "colors"   =>  ["4", "5", "6"],        
  "lessons"  =>  ["7", "8", "9"],
  "comment"  =>  "10",       
];
 
foreach($defaultValues as $key => $items){
  echo "";
  
  // 处理替换值:数组转逗号分隔字符串
  $replaceVal = is_array($replace[$key]) ? implode(',', $replace[$key]) : $replace[$key];
  // 处理搜索值:数组取首个元素(对应默认值数组的单元素结构)
  $searchVal = is_array($search[$key]) ? $search[$key][0] : $search[$key];
  
  if(is_array($items)){
    foreach($items as $child){
      echo str_replace($searchVal, $replaceVal, $child);
    }
  } else {
    echo str_replace($searchVal, $replaceVal, $items);
  }
 
  echo "";  
}

修复说明

  1. 多值转换:将$replace中的数组值用implode(',', ...)转为逗号分隔的字符串,确保替换后输出多值组合。
  2. 变量逻辑修正:移除原代码中冗余的重复str_replace调用,避免数组遍历过程中错误覆盖变量的问题。
  3. 场景区分处理:针对$defaultValues中的数组/非数组项分别处理,保证替换逻辑统一。

内容的提问来源于stack exchange,提问作者Homersx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 04:50:22