php获取未知对象键

时间:2017-04-06 19:10:45

标签: php json object

我有一个看起来像这样的JSON数据库:

"media": {
        "1": {
            "title": "Test Image",
            "description": "This is a test image",
            "thumb": "test.thumb.jpg",
            "full": "img-2007720.jpg"
        },
        "2": {
            "title": "Test2 Image",
            "description": "This is a test image",
            "thumb": "test.thumb.jpg",
            "full": "img-20077202222.jpg"
        }
    }

我有一个可以返回多个已知属性的get函数:

switch ($numArgs) {
    case 1: return Merz::$db->{$args[0]}; break;
    case 2: return Merz::$db->{$args[0]}->{$args[1]}; break;
    case 3: return Merz::$db->{$args[0]}->{$args[1]}->{$args[2]}; break;
    default: return false; break;
}

问题在于我无法将Merz::get('media')分配给变量:

$media = Merz::get('media');
foreach($media as $item) {
}

继续说:“Object of class stdClass could not be converted to string” - 所以我不能做一个foreach,但我可以var_dump

var_dump是:

object(stdClass)[5]
    public '1' => 
       object(stdClass)[4]
         public 'title' => string 'Test Image' (length=10)
         public 'description' => string 'This is a test image' (length=20)
         public 'thumb' => string 'test.thumb.jpg' (length=14)
         public 'full' => string 'img-2007720.jpg' (length=15)
    public '2' => 
       object(stdClass)[2]
         public 'title' => string 'Test2 Image' (length=11)
         public 'description' => string 'This is a test image' (length=20)
         public 'thumb' => string 'test.thumb.jpg' (length=14)
         public 'full' => string 'img-20077202222.jpg' (length=19)

我正试图获得值1和2 - 不用说,这是动态的。

任何帮助都会非常感激......

0 个答案:

没有答案