0

PHPを使用して、「テキスト」タグから各値を取得するにはどうすればよいですか?

これは私のjsonファイルです:

[{"number":1,"pages":33,"height":1188,"width":918,"fonts":[],
"text":[[108,108,23,21,2,"Some Text 1"],[108,131,6,21,2,"Some Text 2.."],[108,154,6,21,2,"Some Text 3.. "]]}]

そして、これはこれまでの私のPHPです。

$data = json_decode(file_get_contents('file.json'));
$object = array();
foreach($data as $index=>$object) {
    foreach($object as $name=>$value) {
           //$output[$name][$index] = $value;

           echo $output[text][0];
           // .........
       echo $output[text][5];

    }
}

ありがとう

4

2 に答える 2

1

次の例を検討してください。

$json = <<<JSON
[{"number":1,"pages":33,"height":1188,"width":918,"fonts":[], "text":[[108,108,23,21,2,"Some Text 1"],[108,131,6,21,2,"Some Text 2.."],[108,154,6,21,2,"Some Text 3.. "]]}]
JSON;

$data = json_decode($json, TRUE);

foreach($data[0]['text'] as $key => $array)
{
  var_dump($array[0], $array[5]);
}

出力

int 108

string 'Some Text 1' (length=11)

int 108

string 'Some Text 2..' (length=13)

int 108

string 'Some Text 3.. ' (length=14)

テキスト結果をループしたい場合は、少なくとも 2 つのループを使用する必要があります。

foreach($data[0]['text'] as $key => $array)
  foreach($array as $text)
    echo $key, ' ', $text, PHP_EOL;

出力

0 108
0 108
0 23
0 21
0 2
0 Some Text 1

1 108
1 131
1 6
1 21
1 2
1 Some Text 2..

2 108
2 154
2 6
2 21
2 2
2 Some Text 3.. 
于 2013-09-06T01:41:19.523 に答える