I want to access the array properties I get as a SOAP-Resonse from a server. I use the php soapclient and and get the following output when using
$response = $client->$action($clientID);
$array = json_decode(json_encode($xml), True);
Unfortunately I cannot get access to the properties such as 'tid', 'answer' etc. How can I do this (I can either use php or c# where I import the result)
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => stdClass Object
(
[tid] => 4103
[tdid] => 191
[qid] => 4103-1
[question] => Wie würden Sie Ihren Gesundheitszustand im Allgemeinen beschreiben ?
[answer] => Ausgezeichnet.
[score] => 100
[date] => 1558593404
[Fields] => Array
(
)
)
[1] => stdClass Object
(
[tid] => 4103
[tdid] => 191
[qid] => 4103-2
[question] => Im Vergleich zum vergangenen Jahr, wie würden Sie Ihren derzeitigen Gesundheitszustand beschreiben ?
[answer] => Derzeit etwas besser als vor einem Jahr.
[score] => 75
[date] => 1558593404
[Fields] => Array
(
)
)
By looking at the result, can you try this?
// assuming $array is the array that you described
$tid = ($array[0])->tid;
because $array is an array of Objects, so i assume to get the data is as simple as using the property accessor ->
. so $tid
you can learn more about php objects here
hope it helps!
-- EDIT --
it should be ($array[1][0])->tid
. Because $array[0] is empty. you can use PHP's array_filter to remove all the empty arrays in $array.
$filtered_array = array_filter($array, function($el){
return count($el) > 0;
});
$filtered_array
should only have non-empty arrays in it. but do take note that array_filter preserves array keys.
then, you can get all the objects with foreach loops.
// say you want to put all tids in an array
$array_of_tids = [];
foreach($filtered_array as $array_of_objects){
foreach($array_of_objects as $response_objects){
$array_of_tids[] = $response_objects->tid;
}
}
the simpler but more unreliable (since i don't know the API response's consistency), would be to just use the index.
$tid_of_first_object = ($array[1][0])->tid
// $tid_of_first_object would be '4103'
let me know if it's still not working!
You have an array of arrays of objects, so to access them you need to use $array[index/key][index/key]
until you reach the level the object's at, then use []->
to access it.
you are presumably going to loop through so inside your loop you would need to do some checks such as is_array
,isset
,is_object
, or a count
to see if there's anything to do, it would look something like this:
foreach($result as $arr) {
if(count($arr) < 1) continue;//there are no entries in this array
foreach($arr as $obj) {
if(isset($obj->tid)) {
//do your stuff
}
}
}
You could possibly simplify this by pulling all the data our using array_column
if you just need the tid, it depends what your goal is - but this should set you in the right direction. You might also look at array_map
depending on what you need