I am new to php specially OOP.
I need to access nested object elements.
MyNewJamaContourServiceGet is the class whose object is $jama
Print_r($jama)gives this object structure :
MyNewJamaContourServiceGet Object
(
[result:JamaContourWsdlClass:private] => JamaContourStructGetItemsFromTextSearchResponse Object
(
[return] => JamaContourStructGetItemsFromTextSearchResponse Object
(
[return] => Array
(
[0] => JamaContourStructWsItem Object
(
[childDocumentTypeId] =>
[createdDate] => 2014-03-08
[currentUserWritePermissions] =>
I tried accessing it like :
var_dump($jama->result->JamaContourWsdlClass);
var_dump($jama['result:JamaContourWsdlClass']['return']['return'][0]['JamaContourStructWsItem']['createdDate']);
It gives NULL. What i'm doing wrong.
I also tried converting it to array with type casting and json_encode(json_decode($jama)); but no luck.
MODIFIED : As suggested by the Dev.Bushido I modified $result to public now i get following object :
MyNewJamaContourServiceGet Object
(
[result] => JamaContourStructGetItemsFromTextSearchResponse Object
(
[return] => JamaContourStructGetItemsFromTextSearchResponse Object
(
[return] => Array
(
[0] => JamaContourStructWsItem Object
(
[childDocumentTypeId] =>
[createdDate] => 2014-03-08T01:21:18.867Z
[currentUserWritePermissions] =>
I tried accessing it with following :
var_dump($jama['result']['return']['return'][0]['createdDate']);
var_dump($jama->{'result'}->{'return'}['return'][0]['JamaContourStructWsItem']['createdDate']);
Why i am still not able to access them as they are not private anymore?
Thanks a lot Dev.Bushido! :) Here is the function which i added in the class to search for key in object to retrieve it's value :
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;
}
Is this function right ? as only echo before foreach get executed and i get key not found.
I updated my function as you said :
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'));
I get the result as :
Inside FindValue1 before if
Notice: Undefined property: MyNewJamaContourServiceGet::$label in /opt/lampp/htdocs/JAMA/jamaContour/test_import.php on line 107
It is null
bool(false)
Please Help!