Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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.
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 'не найдено совпадений';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question