2012-04-25 3 views
-1

배열 값을 가져 오려고합니다. 이 내 배열 값입니다PHP를 사용하여 배열 값을 검색하는 방법

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

이 내 코드입니다 :하지만이 working.why되지 않는 이유는 무엇입니까? overlay.txt 파일에서 값을 검색하는 방법. 나는 모든 칭호를 얻지 못했습니다. overlay.txt에서 제목 값을 얻는 방법을 모르겠습니다. $ 제목이 비어 있습니다. 여기서 $ title 값을 얻기 위해 코드를 변경하고 싶습니다. 어떤 시점에서 당신이 그것을 다시 PHP 배열로 디코드 필요하므로

$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; 
    } 
+1

"이 기능이 작동하지 않음"에 대해 좀 더 구체적으로 설명해야합니다. –

+0

나는 당신이 섞는 것 같아요. 귀하의 json 콘텐츠는 아약스에 의해 PHP로 전송됩니까? "not working"은'$ json'이 null이라는 것을 의미합니까? –

+0

overlay.txt file.Send me PHP 코드에서 값을 다시 얻으려면 ok.how. –

답변

0

완전히 JSON 형식 overlay.txt합니다

[ 
    {"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))); 

오버레이 페이지를
키가 "filecon"인 배열을 0으로 설정합니다.의 두 번째 인수.
http://codeigniter.com/user_guide/general/views.html

+0

아니오, 응답 없음 –

+0

이것은 매우 기본적인 패턴입니다. 나는 당신이 각 공정이 잘되었는지 아닌지 확인해야한다고 생각한다. file_get_contents는 정상입니까? $ this-> load-> view()에서 간단한 변수를 설정하면 잘 동작합니까? – tosin

+0

내 코드 참조 : 이것이 맞습니까 또는 아닙니다? \t \t $ jsonObjects = file ('./ videos/overlay.txt'); foreach ($ jsonObjects as $ json) { $ array = json_decode ($ json, true); $ prints = $ array [ '제목']; echo $ prints; } –

1

당신은, JSON에 배열 인코딩입니다. 실제로 파일에 여러 JSON 객체가 있으므로 각 객체를 개별적으로 디코딩해야합니다. 를 항상 라인 당 하나의 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"} 

을 그 데이터를 저장하는 방법은 그리 신뢰할 만하지 않습니다. ,

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

당신이보기 파일에 $filecon을 사용하려면 :

+0

아니요 작동하지 않습니다. –

+1

좀 더 정확하게하려고 할 수 있습니까? "작동하지 않는다"는 것은 프로그래머가 사용하는 문제 설명이 아니라 *. – deceze

+0

오케이. 매우 근사합니다. 일하고 있습니다. –

관련 문제