0

i have an array say array1(asd,ard,f_name,l_name) now i want to replace some value as

asd with agreement start date

f_name with first name.

l_name with last name.

what i have done is this, but it is not checking for second if condition

  for($i = 0; $i < count($changedvalue);$i++){
  //Check if the value at the 'ith' element in the array is the one you want to change
//if it is, set the ith element to some value
if ($changedvalue[$i] == 'asd')
   {$changedvalue[$i] = 'Agreement Start Date';}
   elseif ($changedvalue[$i] == 'ard')
   {$changedvalue[$i] == 'Agreement Renewal Date';}
 }

3 Answers 3

2

You could do it this way:

foreach ($changedvalue as $key => $value)
{
     switch ($value)
     {
            case('asd'):
                $changedvalue[$key]='Agreement Start Date';
                break;
            case('f_name'):
                $changedvalue[$key]='first name';
                break;
            case('l_name'):
                $changedvalue[$key]='last name';
                break;
     }
}

This way you go through each row in the array and set the value to the new value if the old value was equal to one of the reset values.

Sign up to request clarification or add additional context in comments.

Comments

1

You have a typo in your last statement. '==' should be the assignment operator '='

Comments

0

The problem with your current code is that == in the last line should be =.

However, I would recommend changing your code to something like this:

$valuemap = array(
   'asd' => 'Agreement Start Date',
   'f_name' => 'first name', 
    // and so on...
);

function apply_valuemap($input) {
    global $valuemap;
    return $valuemap[$input];
}

array_map('apply_valuemap', $changedvalue);

This way, it's way easier to add more values you want to replace.

1 Comment

this is what i was thinking of..thnx a lot Cnt +1 your answer, still living with 12 repo points

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.