0

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 LOOPを使用して出力しています

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

どうすればこれを入手できますか 助けてください......

4

3 に答える 3

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
于 2013-08-26T05:23:30.980 に答える
0

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;

もう 1 つの方法は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;
于 2013-08-26T05:40:30.073 に答える
0

このために、 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 で利用可能になりました。

于 2013-08-26T05:15:21.690 に答える