替换字符串中的2个以上不同的单词

I have a string contains shortcodes that I would like to be replaced with array values let suppose if there are 2 different names within a string that I would like to replace with matched array values i.e array('pizza', 'fruit'); is for replacing with (Hello world lorem ipsum [name='apple'] lorem ipsum [name='bread'])

so [name='apple'] to be replaced with fruit and [name='bread'] to be replaced with pizza

Within my current code it is replacing perfectly when the placing is in order of placed values but when i switch the words placing in array it replacing in the order of array sequence

$content = "Hello world this is a lorem ipsum text [shortcode name='generate'] this is used for some [shortcode id='2' name='corusel'] dummy words";
preg_match_all("/\[(.*?)\]/", $content, $matches);

$values = array();

foreach($matches[1] as $match) {
    if(is_array($match)) {
        foreach($match as $value) {
            echo $values = str_replace($match, "[$match]", $match);
            $value2 = explode(" ", $match);

            echo "<br />";
        }
    } else {
        $values[] = str_replace($match, "[$match]", $match);
        $value2 = explode(" ", $match);
        $value3 = explode("=", $value2[1]);
        if(isset($value2[2])) {$value4 = explode("=", $value2[2]);}
        $val1   = str_replace("'", "", $value3[1]);
        if(isset($value4[1])) {$val2   = str_replace("'", "", $value4[1]);}
        if(isset($val2)){$val2;}
        echo "<br />";
    }
}

$text = array('corusel', 'generate');
print_r($values);
echo "<br />";
echo str_replace($values, $text, $content);

output :

Array ( [0] => [shortcode name='generate'] [1] => [shortcode id='2' name='corusel'] ) Hello world this is a lorem ipsum text corusel this is used for some generate dummy words

corusel is replacing generate this is wrong it should replace corusel and generate should be replaced by generate when i switch position it works perfectly

If the substitute can be found in the attribute name (your question is not clear on the matter), your goal can be achieved pretty easily (no need to loop over the matches and explode, etc.):

<?php
$content = "Hello world this is a lorem ipsum text [shortcode name='generate'] this is used for some [shortcode id='2' name='corusel'] dummy words";
$regex = '~\[.*?name=([\'"])([^\'"]+)\1\]~';
# look for name=, followed by single or double quotes
# capture everything that is not a double/single quote into group2
# match the previously captured quotes
# the whole construct needs to be in square brackets

$content = preg_replace($regex, "$2", $content);
echo $content;
// output: Hello world this is a lorem ipsum text generate this is used for some corusel dummy words
?>

See an additional demo on regex101.

To only get the values (i.e. not replacing anything yet) of the name attribute, you can call preg_match_all() after the regex definition:

preg_match_all($regex, $content, $matches);
print_r($matches);
// your name values are in $matches[2]

You can even go further and replace the whole shortcode thingy with anything you want:

<?php
$content = "Hello world this is a lorem ipsum text [shortcode name='generate'] this is used for some [shortcode id='2' name='carousel'] dummy words";
$regex = '~\[.*?name=([\'"])([^\'"]+)\1\]~';

$replacements = array("generate" => "Some generator stuff here", "carousel" => "Rollercoaster");   

$content = preg_replace_callback($regex, 
    function ($match) {
        global $replacements;
        $key = $match[2];
        return $replacements[$key];
    },
    $content
    );
echo $content;

// output: Hello world this is a lorem ipsum text Some generator stuff here this is used for some Rollercoaster dummy words
?>