多维SEO数组,如何提取和打印所有?

时间:2015-10-20 09:00:07

标签: php arrays multidimensional-array foreach

我正在制作一个seo"插件"对于我的应用程序,我设法创建所需的数组...但是当我尝试做一个foreach循环时,它没有输出任何东西......我没有得到任何错误,它可能是foreach循环中的foreach循环?

我的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
                )

        )

)

2 个答案:

答案 0 :(得分:1)

指定$Meta['content']而不是$Meta[1]

此外,您可能有兴趣使用PHP的魔术__set()方法,而不是基本上使用您的Set()方法重新实现它。您可以在PHPDoc中定义支持的@properties。

http://php.net/manual/en/language.oop5.overloading.php#object.set

答案 1 :(得分:-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.'"/>';
            }