2011-07-02 66 views
1

新手在這裏,我的代碼如下輸出這樣的[{"src":"http","src":"http"},{"src":"http","src":"http"}]使用json_encode。然而,我的要求會是這樣[["http","http"],["http","http"]]Facebook的fql結果json_encode陣列

//to get album cover 
     $fql2 = "select src from photo where pid = '" . $values['cover_pid'] . "'"; 
     $param2 = array(
     'method' => 'fql.query', 
     'query'  => $fql2, 
     'callback' => '' 
     ); 

     $fqlResult2 = $facebook->api($param2); 
     $jsarr = array(); 
     foreach($fqlResult2 as $keys2 => $values2){ 
     } 
     if ($values['name'] != 'Profile Pictures'){ 
      $jsarr['src'] = $album['src']; 

       $count += 1; 

     if ($count == 1){ 
     echo "[";} 
     else { 
     echo ",";} 
     echo json_encode($values2); 
     } 
    } 
    echo "]"; 
} 
?> 

我json_encode的輸出是這樣

[{"src":"http:\/\/photos-c.ak.fbcdn.net\/hphotos-ak-snc6\/251383_221168744573533_221167777906963_849177_3220954_s.jpg"},{"src":"http:\/\/photos-d.ak.fbcdn.net\/hphotos-ak-ash4\/248958_221168431240231_221167777906963_849167_3802855_s.jpg"}] 

我如何將它輸出這樣

[["http:\/\/photos-c.ak.fbcdn.net\/hphotos-ak-snc6\/251383_221168744573533_221167777906963_849177_3220954_s.jpg"],["http:\/\/photos-d.ak.fbcdn.net\/hphotos-ak-ash4\/248958_221168431240231_221167777906963_849167_3802855_s.jpg"]] 

回答

1

一個簡單的方法是保存json_encode結果爲變量,然後str_replace{}字符。

所以,你的代碼將變成:

//to get album cover 
    $fql2 = "select src from photo where pid = '" . $values['cover_pid'] . "'"; 
    $param2 = array(
    'method' => 'fql.query', 
    'query'  => $fql2, 
    'callback' => '' 
    ); 

    $fqlResult2 = $facebook->api($param2); 
    $jsarr = array(); 
    foreach($fqlResult2 as $keys2 => $values2){ 
    } 
    if ($values['name'] != 'Profile Pictures'){ 
     $jsarr['src'] = $album['src']; 

      $count += 1; 

    if ($count == 1){ 
    $outputStr .= "[";} 
    else { 
    $outputStr .= ",";} 
    $outputStr .= json_encode($values2); 
    } 
} 
$outputStr .= "]"; 
$outputStr = str_replace("{","[",$outputStr); 
$outputStr = str_replace("}","]",$outputStr); 
echo $outputStr; 
} 
?> 
+0

需要注意的是,你會想要實例'用'$ outputStr =「」 $ outputStr';'第一次使用前的地方(如清潔編碼的一部分)。我沒有這樣做,因爲我不完全確定之前的代碼是什麼(因爲有可能你也想在其中的一部分中使用'outputStr')。 –