2015-10-20 52 views
1

我正在为我的应用程序创建一个seo“插件”,我设法创建所需的数组... ...但是当我尝试执行foreach循环时,它不输出任何内容......并且我没有任何错误,它可能是foreach循环内的foreach循环?多维SEO阵列,如何提取和打印所有?

我的PHP代码:

include 'seo.class.php'; 

$SEO = new SEO(); 
$SEO->Set("Image", "img.png"); 
$SEO->Set("Description", "My description"); 
$SEO->Set("PageTitle", "Page Title"); 
$SEO->Set("Author", "Author"); 
$SEO->build(); 
?> 

<html> 
    <head> 
     <?php echo $SEO->render($SEO->Array); ?> 
    </head> 
</html> 

我的PHP类:

class SEO{ 

    public $SiteName = "Website Name"; 


    public $Author; 
    public $BaseUrl; 
    public $PageUrl; 
    public $PageTitle; 
    public $Description; 
    public $Image; 
    public $Array; 


    function Set($What, $Value){ 
     $this->$What = $Value; 
    } 


    function build(){ 
     $SEO = array(); 

     //For twitter: 
     $SEO['Twitter'] = array(

      'type' => 'name', 
      'content' => array(

      'twitter:description' => $this->Description, 
      'twitter:title' => $this->PageTitle, 
      'twitter:image' => $this->Image, 
      'twitter:creator' => $this->Author) 
      ); 

     //For facebok: 
     $SEO['Facebook'] = array(

      'type' => 'property', 
      'content' => array(

      'og_title' => $this->PageTitle, 
      'og:description' => $this->Description, 
      'og:type' => "article", 
      'og:url' => $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], 
      'og:image' => $this->Image, 
      'og:site_name' => $this->SiteName) 
     ); 



     $this->Array = $SEO; 
    } 

    function render($Array){ 

     foreach($Array as $Location => $Meta){ 


      foreach($Meta[1] as $Property => $Value){ 
       $R .= "<meta ".$Type.'="'.$Property.'" content="'.$Value.'"/>'; 
      } 
     } 

     return $R; 
    } 
} 

阵列输出上print_r($Array);

Array 
(
    [Twitter] => Array 
     (
      [type] => name 
      [content] => Array 
       (
        [twitter:description] => My description 
        [twitter:title] => Page Title 
        [twitter:image] => img.png 
        [twitter:creator] => Author 
       ) 

     ) 

    [Facebook] => Array 
     (
      [type] => property 
      [content] => Array 
       (
        [og_title] => Page Title 
        [og:description] => My description 
        [og:type] => article 
        [og:url] => localhost/seotest.php 
        [og:image] => img.png 
        [og:site_name] => Website Name 
       ) 

     ) 

) 

回答

-1

变化

  foreach($Meta[1] as $Property => $Value){ 
       $R .= "<meta ".$Type.'="'.$Property.'" content="'.$Value.'"/>'; 
      } 

  foreach($Meta[0] as $Property => $Value){ 
       $R .= "<meta ".$Type.'="'.$Property.'" content="'.$Value.'"/>'; 
      }