PHP:我怎样才能得到“Hello World!”在$变量中的这个数组?

时间:2016-12-28 15:09:02

标签: php arrays variables meta

我的阵列有点问题,希望有人可以帮助我:

Array ( 
[0] => Pagekit\Blog\Model\Post Object ( 
    [id] => x 
    [title] => x 
    [slug] => x
    [user_id] => x 
    [date] => DateTime Object ( 
        [date] => 2016-12-28 07:51:02.000000 
        [timezone_type] => 3 
        [timezone] => UTC 
    ) 
    [content] => xxxxxxxxxxxx
    [excerpt] => xxxxxxxxxxxx
    [status] => xxxxxxxxxxxx 
    [modified] => DateTime Object ( 
        [date] => 2016-12-28 09:17:48.000000 
        [timezone_type] => 3 
        [timezone] => UTC 
    ) 
    [comment_status] => 1 
    [comment_count] => 0 
    [user] => Pagekit\User\Model\User Object ( 
        [id] => 1 
        [username] => xxxxxxxxxxxx 
        [password] => xxxxxxxxxxxx 
        [email] => xxxxxxxxxxxx 
        [url] => xxxxxxxxxxxx
        [registered] => DateTime Object ( 
            [date] => 2016-12-15 15:33:36.000000 
            [timezone_type] => 3 
            [timezone] => UTC 
        ) 
        [status] => 1 
        [name] => xxxxxxxxxxxx 
        [login] => DateTime Object ( 
            [date] => 2016-12-27 13:55:45.000000 
            [timezone_type] => 3 
            [timezone] => UTC 
        ) 
        [activation] => [permissions:protected] => [roles] => Array ( 
            [0] => x 
            [1] => x 
        ) 
        [data] => Array ( 
            [admin] => Array ( 
                [menu] => Array ( 
                    [dashboard] => x 
                    [user] => x 
                    [system: system] => x 
                    [blog] => x
                    [system: marketplace] => x
                    [portfolio] => x
                    [site] => x
                ) 
            ) 
        ) 
    ) 
    [comments] => [roles] => Array ( ) 
    [data] => Array ( 
        [title] => [markdown] => 1 [image] => Array ( 
            [src] => storage/bilderbuch/bild.jpg 
            [alt] => bild 
        ) 
        [meta] => Array ( 
            [og:description] => Hello World! 
        ) 
    ) 
)   
)

有人可以解释一下我如何从这里得到Hello World!或其他所有内容:(这个大数组中的最后一行)

[meta] => Array ( 
            [og:description] => Hello World!

$Variable?我知道这可能是一个愚蠢的问题,但我只使用PHP一周或其他东西。

3 个答案:

答案 0 :(得分:2)

您的$variable是一个包含1个项目的数组 此项目是Pagekit\Blog\Model\Post对象,因此首先您需要$variable[0]才能到达目的地。

注意 - 此对象有多个属性,您正在寻找data属性:$variable[0]->data

此属性本身是array,其中meta是其中一个键$variable[0]->data['meta'],这是另一个带有og:description键的数组。

所以你需要的最终变量实际上是

$variable[0]->data['meta']['og:description']

答案 1 :(得分:1)

我相信你已经使用过https://pagekit.com/docs/developer/orm

在您的情况下,您可以遍历结果并返回元数据:

$meta = [];
foreach ($array as $post)
{
    $meta[] = $post->data['meta'];
}
var_dump($meta);

答案 2 :(得分:0)

所以解决方案是:

$test = spliting($posts, 3);
$var1 = $test[0]->data['meta']['og:description'];

这里的分割功能只将整个数组分成3个部分。