用给定的索引替换php数组中的值

Wil*_*ilf 3 php arrays replace

我在使用精确给定索引替换数组中的值时遇到问题.确切地说,我想用一个新数字替换数组[2]中的字符串,但我不知道.所以请查看我的资源:

pos:0 //position of index in array 
id:498 //id of a field
new:6300 //new value to replace in an array
cost:6200-5200-4600-5600-4100 //the array
Run Code Online (Sandbox Code Playgroud)

从上面,我想用"new"中的新字符串替换"cost"中的字符串索引"0",因此预期结果应为:

6300-5200-4600-5600-4100
Run Code Online (Sandbox Code Playgroud)

我试图搜索到处,但没有任何回报,但array_search - 这不是我正在寻找的.请指教.

Mih*_*rga 7

$pos = 0;                            // position
$new = 6300;                         // new value
$cost = '6200-5200-4600-5600-4100';  // string
$cost = explode('-', $cost);         // make it array
$cost[$pos] = $new;                  // change the position $pos with $new
$cost = implode('-', $cost);         // return new $cost
// 6300-5200-4600-5600-4100
Run Code Online (Sandbox Code Playgroud)