Home >Backend Development >PHP Tutorial >如何得到return 的值,

如何得到return 的值,

WBOY
WBOYOriginal
2016-06-23 13:58:211024browse

		public function huancheng($key)		{             $key="永兴村到山水人家";             $str="";		     $keys=explode('到',$key);             $json = @file_get_contents("http://api.map.baidu.com/direction/v1?mode=transit&origin=".$keys[0]."&destination=".$keys[1]."&region=舟山&output=json&ak=gfPnxaaiPyNIhM7c3eZ1kmEL");             $ar = json_decode($json,true);             array_walk_recursive($ar, 'funv');             function funv($v, $k) 			 {			   global $str;               if($k == 'stepInstruction') 			   {$str.= strip_tags($v);} 			 }			 return $str;	         }

怎么把两个函数分开,然后可以得到$str的返回值。


回复讨论(解决方案)

        public function huancheng($key)        {             global $str;             $key="永兴村到山水人家";             $str="";             $keys=explode('到',$key);             $json = @file_get_contents("http://api.map.baidu.com/direction/v1?mode=transit&origin=".$keys[0]."&destination=".$keys[1]."&region=舟山&output=json&ak=gfPnxaaiPyNIhM7c3eZ1kmEL");             $ar = json_decode($json,true);             array_walk_recursive($ar, 'funv');             return $str;            }         function funv($v, $k)          {           global $str;           if($k == 'stepInstruction')            {$str.= strip_tags($v);}          }
为什么要抱着 php 5.2 不放呢?

别岐视php5.2.
第一次见有人这样写东西
 public function huancheng($key)
        {
             
             function funv($v, $k) 
             {
             
             }
             return $str;   
         }

代码看不懂。不好意思。

        public function huancheng($key)        {             global $str;             $key="永兴村到山水人家";             $str="";             $keys=explode('到',$key);             $json = @file_get_contents("http://api.map.baidu.com/direction/v1?mode=transit&origin=".$keys[0]."&destination=".$keys[1]."&region=舟山&output=json&ak=gfPnxaaiPyNIhM7c3eZ1kmEL");             $ar = json_decode($json,true);             array_walk_recursive($ar, 'funv');             return $str;            }         function funv($v, $k)          {           global $str;           if($k == 'stepInstruction')            {$str.= strip_tags($v);}          }
为什么要抱着 php 5.2 不放呢?

主要bae上不支持啊,另外你这样写也报错的Warning: array_walk_recursive() [function.array-walk-recursive]: Unable to call funv()

如果 funv 是作为类的方法出现的,则应
array_walk_recursive($ar, array($this, 'funv'));

如果 funv 是作为类的方法出现的,则应
array_walk_recursive($ar, array($this, 'funv'));

老大,你太给力了!!!!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn