0

I have an array like:-

array(1,3,5,7,10,12)

I want to replace the numbers in the array with other fixed values. Example:-

1 -> 12
2 -> 44
3 -> 32
4 -> 27
5 -> 11
etc. 

What is the best way to change these values in PHP?

3
  • 1
    What logic you are implementing to replace? Commented Jan 13, 2017 at 10:58
  • If you mean for what purpose it will be used? The fixed values are wordpress post id's. I am trying to setup a function for converting values from an imported xml file. The first array is what i have in my xml file. Commented Jan 13, 2017 at 11:34
  • How it would decide 1 -> 12, 3 -> 44? Commented Jan 13, 2017 at 11:35

2 Answers 2

2

I see the way to have a switchcase in a foreach, but i don't know if it is the best way.

foreach ($values as $value){
    switch ($value){
            case 0:
                //make relative change;
                break;
            case 1:
                //make relative change;
                break;
            case 2:
                //make relative change;
                break;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thx, i have used your suggestion.
1

Try array_replace;

array_replace($arr1, $arr2);

Comments

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.