4

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.

1
  • How is you array look? What you has here is not much as PHP code, but normal if you has a array you can access/replace a value by use $array[$index] = $newvalue; Commented Sep 13, 2012 at 8:39

3 Answers 3

8
$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
Sign up to request clarification or add additional context in comments.

4 Comments

@Mihai lorga I've tried your code which seems to be clearest. But the result is a bit weird - the value added up. It becomes six arrays instead of five. Here's the code:code room_rate_updatez.php?rate|502|6200-5200-4600-5600-4100|3=5500 foreach($_GET as $key=>$_val){ list($_name,$_id,$_cost,$_pos)=explode("|",$key); $cost = explode('-', $_val); // make it array $cost[$_pos] = $_cost; // change the position $pos with $new $cost = implode('-', $cost); // return new $cost echo "new cost : ".$cost; //new cost : 5500-6200-5200-4600-5600-4100 }
But it should be $cost = explode('-', $_cost);
@Mihai I've got it. My fault. I confused my own var - swap between $cost and $new so the the array doesn't understand where to replace. Regards,
Easy. Good job, buddy.
1

It's so simple:

<?php

$array = array(
    6200, 
    5200, 
    4600, 
    5600, 
    4100
);

$pos = 0;
$new = 6300;

$array[$pos] = $new;

?>

4 Comments

This is your first post. So it came under beta review - first posts. To review it we have to edit, vote, flag the post. I found no fault in your post. So just edited and checked it as fine. :)
And instead of updating it again as old, you can use rollback function to undo edit.
Thanks for the info, I'm new here and didn't know about "rollback function" ... :)
You need to visit Meta Stack Overflow to know more about Stack Overflow.
0

Try this:

$the_array[$pos] = $new

1 Comment

Added syntax color - but no color appeared... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.