Aditya Kumar
Aditya Kumar

Reputation: 793

Array: replace the element with other predefined values

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';}
 }

Upvotes: 0

Views: 102

Answers (3)

Jeroen
Jeroen

Reputation: 13257

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.

Upvotes: 0

Thomas
Thomas

Reputation: 2984

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.

Upvotes: 2

pankar
pankar

Reputation: 1701

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

Upvotes: 1

Related Questions