2013-08-26 7 views
0

JSON 응답JSON 응답이

[{"id":630770,"t2":"India A","t1":"South Africa A"}, 
{"id":593454,"t2":"Nottinghamshire","t1":"Kent"}, 
{"id":593453,"t2":"Northamptonshire","t1":"Warwickshire"}, 
{"id":593457,"t2":"Sussex","t1":"Worcestershire"}, 
{"id":593451,"t2":"Hampshire","t1":"Derbyshire"}, 
{"id":593456,"t2":"Surrey","t1":"Durham"},{"id":593449,"t2":"Essex","t1":"Lancashire"}, 
{"id":593455,"t2":"Somerset","t1":"Gloucestershire"}, 
{"id":593452,"t2":"Leicestershire","t1":"Middlesex"}, 
{"id":593450,"t2":"Glamorgan","t1":"Yorkshire"}] 

이며, 배열은 다음과 같습니다

Array 
(
    [0] => stdClass Object 
     (
      [id] => 630770 
      [t2] => India A 
      [t1] => South Africa A 
     ) 

    [1] => stdClass Object 
     (
      [id] => 593454 
      [t2] => Nottinghamshire 
      [t1] => Kent 
     ) 

    [2] => stdClass Object 
     (
      [id] => 593453 
      [t2] => Northamptonshire 
      [t1] => Warwickshire 
     ) 

    [3] => stdClass Object 
     (
      [id] => 593457 
      [t2] => Sussex 
      [t1] => Worcestershire 
     ) 

    [4] => stdClass Object 
     (
      [id] => 593451 
      [t2] => Hampshire 
      [t1] => Derbyshire 
     ) 

    [5] => stdClass Object 
     (
      [id] => 593456 
      [t2] => Surrey 
      [t1] => Durham 
     ) 

    [6] => stdClass Object 
     (
      [id] => 593449 
      [t2] => Essex 
      [t1] => Lancashire 
     ) 

    [7] => stdClass Object 
     (
      [id] => 593455 
      [t2] => Somerset 
      [t1] => Gloucestershire 
     ) 

    [8] => stdClass Object 
     (
      [id] => 593452 
      [t2] => Leicestershire 
      [t1] => Middlesex 
     ) 

    [9] => stdClass Object 
     (
      [id] => 593450 
      [t2] => Glamorgan 
      [t1] => Yorkshire 
     ) 

) 

내가

으로 신갈 변수 대신 배열에 결과 싶어
$var = "ON Going Matches $n : $t1 VS $t2\n"; 
2,326,788,

나는이 들어 ........ 내가이 나를 제발 도와주세요 얻을 수있는 방법

답변

2

이 작동합니다 :

$json = <<< EOF 
[{"id":630770,"t2":"India A","t1":"South Africa A"}, 
{"id":593454,"t2":"Nottinghamshire","t1":"Kent"}, 
{"id":593453,"t2":"Northamptonshire","t1":"Warwickshire"}, 
{"id":593457,"t2":"Sussex","t1":"Worcestershire"}, 
{"id":593451,"t2":"Hampshire","t1":"Derbyshire"}, 
{"id":593456,"t2":"Surrey","t1":"Durham"},{"id":593449,"t2":"Essex","t1":"Lancashire"}, 
{"id":593455,"t2":"Somerset","t1":"Gloucestershire"}, 
{"id":593452,"t2":"Leicestershire","t1":"Middlesex"}, 
{"id":593450,"t2":"Glamorgan","t1":"Yorkshire"}] 
EOF; 
$data = "ON Going Matches\n" . implode("\n", array_map(function($m){static $i=1; 
     return $i++.': '.$m['t1'].' VS '.$m['t2'];}, json_decode($json, true))); 
echo $data."\n"; 

출력 :

ON Going Matches 
1: South Africa A VS India A 
2: Kent VS Nottinghamshire 
3: Warwickshire VS Northamptonshire 
4: Worcestershire VS Sussex 
5: Derbyshire VS Hampshire 
6: Durham VS Surrey 
7: Lancashire VS Essex 
8: Gloucestershire VS Somerset 
9: Middlesex VS Leicestershire 
10: Yorkshire VS Glamorgan 
+1

고마워요 ... –

+0

당신을 환영합니다, 다행히 당신을 위해. – anubhava

+1

@anubhava 좋은 oneliner. – Orangepill

0

를 foreach 루프의 그것을 출력

ON Going Matches 
1: South Africa A VS India A 
ON Going Matches 
2: Kent VS Nottinghamshire 
ON Going Matches 
3: Warwickshire VS Northamptonshire 
ON Going Matches 
4: Worcestershire VS Sussex 
ON Going Matches 
5: Derbyshire VS Hampshire 
ON Going Matches 
6: Durham VS Surrey 
ON Going Matches 
7: Lancashire VS Essex 
ON Going Matches 
8: Gloucestershire VS Somerset 
ON Going Matches 
9: Middlesex VS Leicestershire 
ON Going Matches 
10: Yorkshire VS Glamorgan 

를 사용하고 당신이 원하는 것을 얻을 array_reduce를 사용할 수 있습니다.

<?php 
$json = '[{"id":630770,"t2":"India A","t1":"South Africa A"}, 
{"id":593454,"t2":"Nottinghamshire","t1":"Kent"}, 
{"id":593453,"t2":"Northamptonshire","t1":"Warwickshire"}, 
{"id":593457,"t2":"Sussex","t1":"Worcestershire"}, 
{"id":593451,"t2":"Hampshire","t1":"Derbyshire"}, 
{"id":593456,"t2":"Surrey","t1":"Durham"},{"id":593449,"t2":"Essex","t1":"Lancashire"}, 
{"id":593455,"t2":"Somerset","t1":"Gloucestershire"}, 
{"id":593452,"t2":"Leicestershire","t1":"Middlesex"}, 
{"id":593450,"t2":"Glamorgan","t1":"Yorkshire"}]'; 


$data = array_reduce(json_decode($json, true), 
        function(&$str, $item) { 
         static $i = 0; 
         $i++; 
         $str.= $i.". ".$item["t1"]." VS ".$item["t2"]."\n"; 
         return $str; 
        },"ON Going Matches\n" 
     ); 
print $data; 

지금 $str 데이터베이스 노트에 추가 할 수있는 데이터를 포함합니다. 배열 맵 호출 안에서 클로저를 사용하면 PHP 5.3에서 사용할 수있게되었습니다.

+0

당신은 잘하지만 나는 신갈 텍스트 값으로 DB에 삽입하는 데 사용할 것 때문에 ...을 foreach 루프의 신갈 변수 외부에서 응답을 원하고 foreach 문에 당신은 그렇게 할 수 –

+0

10 배를 삽입 배열을 줄이면 .... 대답으로 업데이트 할 수 있습니다 – Orangepill

+0

array_reduce 예제로 업데이트되었습니다. – Orangepill

0

foreach

$json = json_decode('[{"id":630770,"t2":"India A","t1":"South Africa A"},{"id":593454,"t2":"Nottinghamshire","t1":"Kent"},{"id":593453,"t2":"Northamptonshire","t1":"Warwickshire"},{"id":593457,"t2":"Sussex","t1":"Worcestershire"},{"id":593451,"t2":"Hampshire","t1":"Derbyshire"},{"id":593456,"t2":"Surrey","t1":"Durham"},{"id":593449,"t2":"Essex","t1":"Lancashire"},{"id":593455,"t2":"Somerset","t1":"Gloucestershire"},{"id":593452,"t2":"Leicestershire","t1":"Middlesex"},{"id":593450,"t2":"Glamorgan","t1":"Yorkshire"}]'); 

$answer = "On Going Matches:<br>\n"; 
$index = 0; 
foreach($json as $jsonelement) 
{ 
    $index++; 
    $line = $index .": ". $jsonelement->t1 ." VS ". $jsonelement->t2 . " <br>\n"; 
    $answer = $answer . $line; 
} 
echo $answer; 

또 다른 방법에서 echo "On Going Matches:<br>\n";을 받기는 각 변환 json_decode('your_json_string here', true)을 사용하는 것입니다 배열에있는 객체를 연관 배열에 추가합니다.

$answer = "On Going Matches:<br>\n"; 

$index = 0; 
foreach($json as $jsonelement) 
{ 
    $index++; 
    $line = $index .": ". $jsonelement['t1'] ." VS ". $jsonelement['t2'] . " <br>\n"; 
    $answer = $answer . $line; 
} 
echo $answer; 
+0

작동하지 않습니다. 또한 작동하지 않았지만 대답 1과 2는 저에게 좋습니다 –