2014-10-24 2 views
0

다음 출력을 json으로 가져 왔습니다. 어떻게하면 DND_status 만 에코 할 수 있습니까? 다음은 코드입니다.PHP에서이 json에서 값을 반향하는 방법

JSON 출력 :

Array ([0] => Array ([mobilenumber] => 9809657248 [DND_status] => on [activation_date] => 09-10-2014 17:23 [current_preference] => 0 [preference_history] => Array ([0] => Array ([preference_date] => 20-08-2012 [preference] => 0 [status] => Active) [1] => Array ([preference_date] => 13-08-2012 [preference] => 0 [status] => Active) [2] => Array ([preference_date] => 17-07-2012 [preference] => 0 [status] => Active) [3] => Array ([preference_date] => 16-07-2012 [preference] => 0 [status] => Active) [4] => Array ([preference_date] => 09-07-2012 [preference] => 0 [status] => Active)))) 

PHP의 CODE : 어레이를 판독하여

<?php 
$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"X-Mashape-Key: xd7lpM6y3ImshZOdNfobRDPhiBU4p100Q2JjsnaAlOTxVWrkwZ"    
) 
); 

$context = stream_context_create($opts); 
    $res = file_get_contents('https://dndcheck.p.mashape.com/index.php?mobilenos=9809657248', false, $context); 
    $res = (json_decode($res, true)); 
    print_r ($res);   // print the above json 
    echo $res['DND_status']; //not working 
    echo $res->DND_status; //not working 
?> 
+2

'foreach는 ($ something_else로 $ 뭔가를) {...}' –

+1

그 중첩 된 배열 –

+0

그래서 어떻게 DND_status 인쇄하려면이 사용합니까 ?? –

답변

2

:

:
<?php 

$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"X-Mashape-Key: xd7lpM6y3ImshZOdNfobRDPhiBU4p100Q2JjsnaAlOTxVWrkwZ"    
) 
); 

$context = stream_context_create($opts); 
$res = file_get_contents('https://dndcheck.p.mashape.com/index.php?mobilenos=9809657248', false, $context); 
$res = (json_decode($res, true)); 
print_r ($res);   // print the above json 

echo $res[0]['DND_status']; // what you need by reading in array 

?> 

또는 foreach 루프와

<?php 

$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"X-Mashape-Key: xd7lpM6y3ImshZOdNfobRDPhiBU4p100Q2JjsnaAlOTxVWrkwZ"    
) 
); 

$context = stream_context_create($opts); 
$res = file_get_contents('https://dndcheck.p.mashape.com/index.php?mobilenos=9809657248', false, $context); 
$res = (json_decode($res, true)); 
print_r ($res);   // print the above json 


foreach ($res as $key => $value) { 
    echo $value['DND_status']; // what you need by foreach method 
} 
?> 
0

작동해야이 시도 : 주석에서 언급 한 바와 같이

echo $res[0]['DND_status']; 
-1

는, foreach는 루프를 사용

foreach ($opts as $opt) { 
    foreach ($opt as $op) { 
     echo $op['method']; 
     echo $op['header']; 
    } 
} 
관련 문제