0

How would you write a sorting routine in PHP that would replace the values of an array's keys, with another array's key value's if the former array's key value's are out of order.

$array1 = ['section2', 'section3', 'section1'];
$array2 = ['content for section1', 'content for section2', 'content section3'];

How could I replace the values of $array1 with the corrosponding content in $array2?

So I guess what I'm asking is how can I make $array1 display the following....

content for section2
content for section3
content for section1

in that order.

4
  • 1
    check this manuals w3schools.com/php/func_string_str_replace.asp php.net/manual/en/function.str-replace.php Commented May 8, 2016 at 23:11
  • What relation between two array ? content for section2 is a sample value or always end with word section2 ? Commented May 8, 2016 at 23:13
  • If there is not any relationship between two array orders your question hasn't any solution Commented May 8, 2016 at 23:14
  • Somewhere logical error hidden. Is this an actual code? What if content for section1 has a link or reference for section2? Why you don't want to have the second array like this: $array2 = ['section1'=>'content for section1', 'section2'=>'content for section2', ... etc] ? You need to have strong relationship between this two arrays. Commented May 9, 2016 at 0:14

2 Answers 2

1

Loop both arrays and check if the value of $array1 exists while looping $array2, if so, change the value of $array1 to the $array2 value based on the key, so you can keep the order, i.e.:

$array1 = ['section2', 'section3', 'section1'];
$array2 = ['content for section1', 'content for section2', 'content for section3'];

foreach($array1 as $key => $value){
    foreach($array2 as $key2 => $value2){
        if(preg_match("/$value/", $value2)){
            $array1[$key] = $value2;
        }
    }
}

print_r($array1);

Output:

Array
(
    [0] => content for section2
    [1] => content for section3
    [2] => content for section1
)

Ideone Demo

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

1 Comment

I think that 1. $key2 should not be declared and 2. there should be a break in the inner loop.
0

A nested loop is generally not desirable from a performance perspective. If you want to use one, I recommend using a break statement after a match is found to prevent useless iterations. If not harmful to subsequent scripting, you can improve performance a little more by consuming/un-setting matched values from the haystack array -- again, fewer iterations. Demo

foreach ($array1 as $needle) {
    foreach ($array2 as $i => $haystack) {
        if (str_contains($haystack, $needle)) {
            echo $haystack . "\n";
            unset($array2[$i]);
            break;
        }
    }
}

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.