【发布时间】:2018-06-12 08:10:11
【问题描述】:
如何在每个循环中完成我的变量?
$string = "Our products are shoes, pants, shirts."
$products = get_post_meta( post_id, 'products', true );
$matches = get_post_meta( post_id, 'matches', true );
$newPhrase = '';
foreach ($matches as $match){
$id = searchForId($match, $products); // searching for the right id
$newPhrase = str_replace($match, $products[$id]['sku'], $string);
}
// $newPhrase should be "Our Products are 3, 4, 9."
虽然每次 foreach 都会更改变量,但它总是会重新开始并采用旧字符串。例如:“我们的产品是鞋子,裤子,9。”
【问题讨论】: