-1

配列値を取得したい。これは私の配列値です:

overlay.txt:

{"title":"sss","description":"sss","code":"sss"}
{"title":"trtr","description":"trtr","code":"tyrytr"}
{"title":"ret54","description":"56tr","code":"ty76"}
{"title":"rgfdg","description":"dfgdfg","code":"dfgdfg"}
{"title":"asfafdsf","description":"sdfsdf","code":"sdfsdfsdf"}

これは私のコードです:しかし、これは機能していません。なぜですか?overlay.txtファイルから値を取得する方法。すべてのタイトル値を取得できませんでした。overlay.txtからタイトル値を取得する方法がわかりません。$titleが空で表示されています。$title値を取得するためにコードを変更したい場所。

    $info = array();
    $folder_name = $this->input->post('folder_name');
    $info['title'] = $this->input->post('title');
    $info['description'] = $this->input->post('description');
    $info['code'] = $this->input->post('code');
    $json = json_encode($info);
    $file = "./videos/overlay.txt";
    $fd = fopen($file, "a"); // a for append, append text to file
    fwrite($fd, $json);
    fclose($fd); 
    $filecon = file_get_contents('./videos/overlay.txt', true);
    $this->load->view('includes/overlays',$filecon);

    //overlays page;
    foreach($filecon as $files)
    {
        $title=$files['title'];
        echo $title;
    }
4

2 に答える 2

1

配列をJSONにエンコードしているため、ある時点で、配列をPHP配列に再度デコードする必要があります。実際にはファイルに複数のJSONオブジェクトがあるため、それぞれを個別にデコードする必要があります。1行に常に1つのJSONオブジェクトであるとすると、次のようになります。

$jsonObjects = file('overlay.txt', FILE_IGNORE_NEW_LINES);

foreach ($jsonObjects as $json) {
    $array = json_decode($json, true);
    echo $array['title'];
    ...
}

シリアル化されたJSON内に改行がある場合、これは非常に迅速に中断されます。例:

{"title":"ret54","description":"foo
bar","code":"ty76"}

データを保存するその方法はあまり信頼できません。

于 2012-04-25T03:50:25.100 に答える
0

overlay.txtを完全にjson形式にします。

[
  {"title":"sss","description":"sss","code":"sss"},
  {"title":"trtr","description":"trtr","code":"tyrytr"},
  ...
]

そしてこれを試してください:

$raw = file_get_contents('./videos/overlay.txt', true);
$this->load->view('includes/overlays', array("filecon" => json_decode($raw)));

オーバーレイページ:

<?php
foreach($filecon as $files) {
    echo $files['title'];
}
?>

$fileconビューファイルで使用する場合は、の2番目の引数
にキー「filecon」を持つ配列を設定します。http://codeigniter.com/user_guide/general/views.html$this->load->view()

于 2012-04-25T04:05:27.487 に答える