当函数返回字符串时,Echo $string不打印数组元素


echo $string does not print array elements while string returned by a function

如何获得

    function render($string, $array)
    {
        $pattern = '/[^{{=]*['w][}}$]/';
        preg_match_all($pattern, $string, $matches);
        foreach($matches as $tt)
        {
            $index = '';
            foreach($tt as $match1){
                $match = str_replace('}', '', $match1);
                if (strpos($match,'.') !== false)
                {
                    $string_parts = explode('.', $match);
                    //print_r($string_parts);
                    foreach($string_parts as $part)
                    {
                        $index .="['".$part."']"; 
                    }
                }
                else
                {
                    $index ="['".$match."']";
                }
                //echo '$array'.$index;
                $new_str =  str_replace("{{=".$match."}}", '{$array'.$index.'}' , $string);
                    $index = '';
                //echo $new_str;
                $string = $new_str;
            }
        }
        return $string;
    }

    $arr = [
            'site'=>'smartprix',
            'users'=>[
                        ['name'=>'user1', 'contact'=>'1234'],
                        ['name'=>'user2', 'contect'=>'4321']
                     ],
            'location'=>['address'=>['pincode'=>'123456', 'city'=>'Noida'],
                         'near'=>'mamura']
            ];
    $array = $arr;
    //echo "{$array['site']}"; //It is printing smartprix
    $string = "{{=site}} is located at pincode {{=location.address.pincode}}";
    echo render($string, $array);

//它打印"{$array['site']}是位于pincode {$array['location']['address']['pincode']}"为什么它不将$array['site']转换为值。我读了php手册,得到了一些参考{}不工作的返回字符串,那么什么是方法,让我可以打印数组值返回字符串后?

可以打印期望的字符串is。

 echo $string = $array['site']." is located at pincode ".$arr['location']['address']['pincode'];