W
W
workRave2016-09-20 13:40:13
PHP
workRave, 2016-09-20 13:40:13

How to access an array element? Why doesn't it work?

Good afternoon!
I request statistics from zadarma IP telephony:

$answerObject = json_decode($answer);
if ($answerObject->status == 'success') {
    print_r($answerObject->stats);
} else {
    echo $answerObject->message;
}

As a result, in the html source code, an array (?): https://gist.github.com/anonymous/c7428d6f40475bbf...
1) How can I iterate over such an array? (I tried several options, it doesn't work)
2) How to see the value of [pbx_call_id], let's say 14 elements for example?
PS>> The task, to pass on an array if [pbx_call_id], coincides with necessary to me to look at [sip] and [disposition], and depending on values ​​to execute certain action.
3 days I suffer ... help :(

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DevMan, 2016-09-20
@workRave

How can I iterate over such an array?
foreach($answerObject->stats as $item) {
    echo $item->pbx_call_id, PHP_EOL;
}
you have an array of objects. therefore, you work with an array as with an array, and with objects as with an object.

M
Maxim Timofeev, 2016-09-20
@webinar

How to see the value of [pbx_call_id], say 14 elements for example?

$arr = $answerObject->stats;
echo $arr[14][pbx_call_id];

$nuznii_pbx_call_id = 123;
$arr = $answerObject->stats;
$sip = false;
foreach($arr as $one){
if($one['pbx_call_id'] == $nuznii_pbx_call_id){
$sip =$one['sip'];
}
}
if($sip){
echo $sip;
}else{
echo 'не найдено совпадений';
}

V
Vitaly, 2016-09-20
@vshvydky

$answerObject is not an object but an array of objects, so answerObject[1]->status
php.net/manual/ru/control-structures.foreach.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question