2011-02-02 6 views
2

나는 (이 클래스 내에서의)이 같은 PHP에서 JSON 배열을 만드는 오전 :의 PHP JSON 배열 문제

$STH = $DBH->query("SELECT ID,title,content FROM table WHERE ID= '".$ID."' "); 
$querycount = $STH->rowCount(); 
if($querycount!=0): 
    $STH->setFetchMode(PDO::FETCH_OBJ); 
    while($row = $STH->fetch()) : 
     $thread[$row->ID]['title'] = $row->title; 
     $thread[$row->ID]['content'] = $row->content; 
    endwhile; 
    $this->querycount = $querycount; 
    $this->thread = json_encode($thread); 
else: 
    $this->querycount = $querycount; 
endif; 

하지만 "이상한"얻을 수가는 다른 쪽 끝을 디코딩. 나는 실제로 키에 의해 배열을 참조 할 수 없다.

$threads = new af_threads($key,$DBH); 
$threads = json_decode($threads->thread); 
foreach($threads as $key1=>$thread): 
    foreach($thread as $key2=>$val): 
     echo $key2; 
     echo $val; 
    endforeach; 
    echo '<hr />'; 
endforeach; 

(af_threads 클래스 이름과 $ 키 ID 테이블 쿼리의 ID 인 경우) 그러나 그것은 나를 내버려은이 같은 것입니다 :

지금까지 내가이 작업을 수행하게됩니다

$threads[$key]['title'] 

이유가 확실하지 않습니다. 클래스가 빌드됨에 따라 더 많은 행 등으로 특정 위치에서 특정 행을 호출해야합니다. 예 : $threads[$key]['newrow'], $threads[$key]['newrow2'], 행을 조작하는 경우는 substr()입니다. 등

내가 모든 잘못도 있고 ... 내가 JSON없이 배열을 에코 단지 수 알지만, 결국 나는 JQuery와해서 getJSON()과 도메인 간 기능을 추가하려면? 기본적으로

+0

생성 된 JSON의 모습은 어떻습니까? 예상했던 것과 동일하다는 것을 확인 했습니까? –

+0

내 json이 내게 뭔가를 제공합니다 (1 & 2는 배열 생성에서 tableID의 $ row-> ID입니다) : stdClass Object ([1] => stdClass Object ([title] => this is [content] => Lorem (2) => stdClass Object ([title] => [content] => Lorem ipsum dolor sitet, consectetur adipiscing elit). Vivamus interdum varius urna. Donec quis turpis elit.) –

답변

3

, json_decode() 반환 stdClass

$threads = json_decode($threads->thread, true) 

과 두 번째 줄을 수정하면 연관 배열을 얻을 것이다 예를 들어, 키가 $threads[$key]['title'] 그것을 참조 할 수 있습니다.

+0

David - 과거에는 항상 "직렬화"되어 있고 json으로 재생되지 않았지만 "true" –