Reputation: 2315
I have a problem on replacing a value in array with exact given index. To be exact, I'd like to replace a string in array[2] with a new number but I don't have an idea. So please have a look on my resources :
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
From above, I'd like to replace a string indexes "0" in "cost" with a new string from "new" so the expected result should be :
6300-5200-4600-5600-4100
I tried to search for everywhere but there's nothing return but array_search - which is not what i'm looking for. Please advise.
Upvotes: 4
Views: 15685
Reputation: 1153
It's so simple:
<?php
$array = array(
6200,
5200,
4600,
5600,
4100
);
$pos = 0;
$new = 6300;
$array[$pos] = $new;
?>
Upvotes: 1
Reputation: 39704
$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
Upvotes: 8