2014-05-20 41 views
0

我收到的名字和一些文件的鏈接來自外部網站,並使用下面的代碼將其轉換成JSON字符串:沒有得到期望的輸出通過轉換PHP數組轉換爲JSON

<?php 

include('simple_html_dom.php'); 
$f = $_GET['f']; 
$w = "www.something.com"; 
$dom = file_get_html($w); 
$arr = array("file" => array()); 

foreach($dom->find('div[id=file_html] div[id=right_data]') as $top){ 

$n = $top->find('div', 0)->childNodes(0)->innertext; 
$l = $top->find('div', 2)->childNodes(0)->childNodes(0)->childNodes(0)->href; 
$jj[] = array('name' => $n, 'link' => $l); 


} 
array_push($arr['file'], $jj); 

echo json_encode($arr); 

?> 

我期望的輸出是

{"songs":[{"name":"file1","link":"link1"},{"name":"file2","link":"link2"}]} 

但輸出我得到的是:

{"songs":[[{"name":"file1","link":"link1"},{"name":"file2","link":"link2"}]]} 

可能有人善意指出ERR或在我的代碼? 謝謝

+0

迭代後嘗試'$改編[ '文件'] = $ JJ;' – Cyclonecode

回答

0

刪除這一條,

$arr = array("file" => array()); 

foreach()

foreach($dom->find('div[id=file_html] div[id=right_data]') as $top){ 
    // as it is.... 
} 

$arr['file'] = $jj; 
+0

我需要的 「文件」 JSONArray ..有沒有其他辦法可以在那裏? –

+0

謝謝,那工作:) –