我有一个产品变体组合 ID。连字符 ( - ) 字符串之间的字符表示变体选项 ID。我想根据主要组合 ID 为自由变体选项制作其他 ID 的副本。我的代码:function find_replace($array, $find, $replace){ $array = array_replace($array, array_fill_keys( array_keys($array, $find), $replace ) ); return $array;}function get_var_key($array, $value){ $key_name=false; foreach ($array as $n=>$c) if (in_array($value, $c)) { $key_name=$n; break; } return $key_name;}$get_free_keys = array( "var1" => array( "free1", "free2" ), "var2" => array( "free3", "free4" ));$main_combine = "a1-b1-free1-c1-d1-free3";$main_combine_explode = explode("-", $main_combine);for($i=0; $i < count($main_combine_explode); $i++){ $get_key_by_value = get_var_key($get_free_keys, $main_combine_explode[$i]); // return "var1" or "var2" foreach($get_free_keys[$get_key_by_value] as $values){ $find_combine = find_replace($main_combine_explode, $main_combine_explode[$i], $values); $combines[] = implode("-", $find_combine); }}print_r($combines); 错误的结果:Array( [0] => a1-b1-free1-c1-d1-free3 // main combine (ok) [1] => a1-b1-free2-c1-d1-free3 // ok [2] => a1-b1-free1-c1-d1-free3 // wrong [3] => a1-b1-free1-c1-d1-free4 // wrong)结果不正确我想得到以下结果:Array( [0] => a1-b1-free1-c1-d1-free3-e1 // $main_combine [1] => a1-b1-free1-c1-d1-free4-e1 [2] => a1-b1-free2-c1-d1-free3-e1 [3] => a1-b1-free2-c1-d1-free4-e1) 或者Array( [var1] => Array ( [0] => a1-b1-free1-c1-d1-free3 // $main_combine [1] => a1-b1-free2-c1-d1-free3 ) [var2] => Array ( [0] => a1-b1-free1-c1-d1-free4 [1] => a1-b1-free2-c1-d1-free4 )) 谢谢你。
1 回答
慕妹3146593
TA贡献1820条经验 获得超9个赞
您可以使用get_combinations和str-replace并执行以下操作:
$template = "a1-b1-@FIRST@-c1-d1-@SECOND@-e1";
foreach (get_combinations($get_free_keys) as $e) {
$res[] = str_replace(['@FIRST@', '@SECOND@'], $e, $template);
}
现场示例:3v4l
- 1 回答
- 0 关注
- 162 浏览
添加回答
举报
0/150
提交
取消