无法访问嵌套的Object元素

时间:2014-04-11 14:48:45

标签: php arrays oop object

我是php的新手。 我需要访问嵌套的对象元素。 MyNewJamaContourServiceGet是其对象为$ jama的类 Print_r($jama)给出了这个对象结构:

MyNewJamaContourServiceGet Object
(
    [result:JamaContourWsdlClass:private] => JamaContourStructGetItemsFromTextSearchResponse Object
        (
            [return] => JamaContourStructGetItemsFromTextSearchResponse Object
                (
                    [return] => Array
                        (
                            [0] => JamaContourStructWsItem Object
                                (
                                    [childDocumentTypeId] =>
                                    [createdDate] => 2014-03-08
                                    [currentUserWritePermissions] =>

我尝试过访问它:

 var_dump($jama->result->JamaContourWsdlClass);
   var_dump($jama['result:JamaContourWsdlClass']['return']['return'][0]['JamaContourStructWsItem']['createdDate']);

它给出了NULL。我做错了什么。 我也尝试使用类型转换和json_encode(json_decode($jama));将其转换为数组,但没有运气。

修改: 正如Dev.Bushido建议我将$ result修改为public,现在我得到以下对象:

MyNewJamaContourServiceGet Object
(
    [result] => JamaContourStructGetItemsFromTextSearchResponse Object
        (
            [return] => JamaContourStructGetItemsFromTextSearchResponse Object
                (
                    [return] => Array
                        (
                            [0] => JamaContourStructWsItem Object
                                (
                                    [childDocumentTypeId] =>
                                    [createdDate] => 2014-03-08T01:21:18.867Z
                                    [currentUserWritePermissions] =>

我尝试通过以下方式访问它:

var_dump($jama['result']['return']['return'][0]['createdDate']);

 var_dump($jama->{'result'}->{'return'}['return'][0]['JamaContourStructWsItem']['createdDate']);

为什么我仍然无法访问它们,因为它们不再是私有的?

非常感谢Dev.Bushido! :) 这是我在类中添加的函数,用于搜索对象中的键以检索它的值:

    class MyNewJamaContourServiceGet extends JamaContourServiceGet{



            public function findValue($jama, $key)
            {
    #           if(array_key_exists($key, $jamaContourServiceGet))
    #           return true;
                    echo "before foreach";
                    foreach($jama as $key1 => $value1)
                    {
                            echo "Inside foreach Key1 value is : ". $key1;
                            echo "Vlaue1 is : ". $value1;
                            if (in_array($key, $value1))
                            {
                                    echo "return value1 is : ". $value1;
                                    return $value1;
                            }
                            elseif(is_array($value1))
                            {
                                    echo "Inside elseif value1 is an ARRAY : ". $value1;
                                    return findValue($value1, $key);
                            }
                    }

             return false;
            }
}

$key ="label";
$r1=$jama->findValue($jama,$key);

if($r1)
{
    echo "The element is in the array" . "\n";
    echo $key ." : " . $r1 . "\n";
} else  {
            echo "Key not found ";
            echo $r1;
        }

这个功能对吗?因为只有echo before foreach被执行而且我找不到密钥。

我按照你的说法更新了我的功能:

class MyNewJamaContourServiceGet extends JamaContourServiceGet{

  private $test;
        public function Add($add){
                $this->test=$add;
        }

        public function FindValue1($object, $key)
        {
                echo "Inside FindValue1 \n";
                echo "before if \n ";
            if (is_null($object->{$key})){
                echo "It is null \n";
                    return false;
                } else if (is_object($object->{$key})){
                        echo "inside else if \n";
                        return FindValue1($object->{$key}, $key);
                  }
                    return $object->{$key};
        }
}

$test = new MyNewJamaContourServiceGet($wsdl);
$testTest = new MyNewJamaContourServiceGet($wsdl);

$testTest->Add(0);
$test->Add($testTest);

var_dump($test->findValue1($test,'label'));

我得到的结果为:

在FindValue1中 之前如果

Notice: Undefined property: MyNewJamaContourServiceGet::$label in /opt/lampp/htdocs/JAMA/jamaContour/test_import.php on line 107
It is null
bool(false)

请帮助!

1 个答案:

答案 0 :(得分:2)

对象的属性访问方式与您现在正在进行的操作不同。

尝试:

var_dump(var_dump($jama->result->return->return[0]->createdDate);

这是你想要的功能:

public function FindValue($object, $key)
{
    if (is_null($object->{$key})){
        return false;
    } else if (is_object($object->{$key})){
        return $this->FindValue($object->{$key}, $key);
    }

    return $object->{$key};
}