1

PHPで読みたいjsonファイルがあります

http://rh.ernestek.cz.cc/webeast/static.txt

「id」と「key」を抽出したかった

次のようなテーブルを作成できるように、IDとキーをループしたかったのです。

IDキー
1 9d07d681e0c1e294264724f7726e6f6f29
3 9cd1e3a4a04b5208862c3140046f73b515
..。

次のコードを使用して最初のIDを抽出し、キーアウトしようとしましたが、うまくいきませんでした。

    <?php
    $json = file_get_contents('static.txt');
    $json_decoded = json_decode($json);
    echo "ID: ".$json_decoded->static->1->id."<br />";
    echo "Key: ".$json_decoded->static->1->key."<br />";
    ?>

私が間違っていることはありますか?何か案は?ありがとう!

4

2 に答える 2

1

jsonを配列としてデコードし(true2番目のパラメーターとして渡す)、次のように配列をループします

<?php
$json = file_get_contents('static.txt');
$json_decoded = json_decode($json, true);
foreach ($json_decoded['static'] as $item) {
     echo 'ID: ', $item['id'], '<br/>';
     echo 'Key: ', $item['key'], '<br/>';
}
于 2013-03-27T14:14:06.187 に答える
0

で使用trueするjson_decode()と、すべてのオブジェクトが配列に変換されます。

$json = file_get_contents("http://rh.ernestek.cz.cc/webeast/static.txt");
$json_decoded = json_decode($json, true); // return array

$table = array();
foreach($json_decoded["static"] as $array){
    $table[$array["id"]] = $array["key"];
}

//creating the table for output
$output = '<table border="1"><tr><td>ID</td><td>Key</td></tr>';
foreach($table as $id => $key){
    $output .= "<tr><td>$id</td><td>$key</td></tr>";
}
$output .= '</table>';
echo $output;
于 2013-03-27T14:17:34.877 に答える