从多维PHP数组中提取特定数据

时间:2012-08-14 06:40:36

标签: php arrays multidimensional-array

我试图从这个多维数组中包含的每个数组中提取“消息”数据,但是无法弄清楚如何在数组中获得足够深的数据来提取它。我正在使用的页面以这种方式返回数据,所以我无法编辑它以便以不同方式返回。

Array
(
[0] => stdClass Object
    (
        [revision_cache] => stdClass Object
            (
                [revision] => 14
                [email] => email@email.com
                [changed_dirs] => Array
                    (
                    )

                [account_id] => 133673
                [message] => Deleted unneeded help desk files.
                [hash_id] => 
                [author] => asdfasfd
                [too_large] => 
                [user_id] => 308011
                [repository_id] => 401677
                [changed_files] => Array
                    (
                        [0] => Array
                            (
                                [0] => hdeskdelete.php
                                [1] => delete
                            )

                        [1] => Array
                            (
                                [0] => hdeskrespond.php
                                [1] => delete
                            )

                        [2] => Array
                            (
                                [0] => hdesktickets.php
                                [1] => delete
                            )

                    )

                [time] => 2012/08/13 17:01:58 -0500
                [changed_properties] => 
            )

    )

[1] => stdClass Object
    (
        [revision_cache] => stdClass Object
            (
                [revision] => 13
                [email] => email@email.com
                [changed_dirs] => Array
                    (
                    )

                [account_id] => 133673
                [message] => Major updates and restyling of Help Desk system.
                [hash_id] => 
                [author] => asdfasdfasdf
                [too_large] => 
                [user_id] => 308011
                [repository_id] => 401677
                [changed_files] => Array
                    (
                        [0] => Array
                            (
                                [0] => members/hdeskdelete.php
                                [1] => delete
                            )

                        [1] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => helpdesk.php
                                        [1] => members/helpdesk.php
                                        [2] => 3
                                    )

                                [1] => copy
                            )

                        [2] => Array
                            (
                                [0] => members/hdeskrespond.php
                                [1] => delete
                            )

                        [3] => Array
                            (
                                [0] => members/hdesktickets.php
                                [1] => delete
                            )

                        [4] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => hdeskdelete.php
                                        [1] => members/hdeskdelete.php
                                        [2] => 3
                                    )

                                [1] => copy
                            )

                        [5] => Array
                            (
                                [0] => members/helpdesk.php
                                [1] => delete
                            )

                        [6] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => hdeskrespond.php
                                        [1] => members/hdeskrespond.php
                                        [2] => 3
                                    )

                                [1] => copy
                            )

                        [7] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => hdesktickets.php
                                        [1] => members/hdesktickets.php
                                        [2] => 3
                                    )

                                [1] => copy
                            )

                    )

                [time] => 2012/08/13 17:01:20 -0500
                [changed_properties] => 
            )

    )

[2] => stdClass Object
...

基本上,这个数组块会持续一段时间。每次“消息”是关键时,我只需要提取值。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

在检查消息是否存在时循环遍历数组中的所有元素,如下所示:

foreach($inputArray as $element)
{
    if( ! empty($element->revision_cache->message)
    {
        //do something with it
    }
}

答案 1 :(得分:0)

这应该这样做:

$message = $arrayname[0]["revision_cache"]["message"];

答案 2 :(得分:0)

尝试

foreach($array as $item){
   echo $item->revision_cache->message."<br>";
}