单PHP变量JSON响应

问题描述:

JSON响应是单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"}] 

在PHP中使用JSON DECODE虽然,数组是:

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"; 

它应该返回

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 

我使用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 

我怎样才能得到这个请大家帮我........

这应该工作:

$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"; 

OUTPUT:

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不错的在线游戏。 – Orangepill

为此,您可以使用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

你是对的,但我想在foreach循环中的葛变量之外响应......因为我会用它插入到数据库作为葛文本价值和它的foreach插入10倍 –

+0

你可以这样做与数组减少....让我通过回答更新 – Orangepill

+0

更新与array_reduce示例。 – Orangepill

获取echo "On Going Matches:<br>\n";出的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; 

另一种方法是使用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对我很好 –