How can I change a value in an array?

How to change a value in an array using PHP?

  • I have an array like this: Array ( [error] = Array ( [0] = Password field is required. [1] = Sorry, unrecognized username or password. <a href="/user/password">Have you forgotten your password?</a> ) ) In the above array I need to add an class in the link. My expected output should be like: Array ( [error] = Array ( [0] = Password field is required. [1] = Sorry, unrecognized username or password. <a href="/user/password" class="popups">Have you forgotten your password?</a> ) ) How do I change the array value?

  • Answer:

    You could use a function like str_replace on it before printing the link to the page.

Fero at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

$array['error'][1] = ..., but from the sound of it, I think you might want to change the value wherever it was set originally. In that case, just search your code for Sorry, unrecognized username or password. or something.

Kenaniah

$somearray['somekey'] = 'newvalue'; $somearray['somekey']['otherkey'] = 'othernewvalue'; ... basic PHP...

Marc B

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.