facebook fql 结果到 json_encode 数组

发布于 2024-11-18 04:52:01 字数 1457 浏览 2 评论 0原文

新手,我下面的代码输出如下 [{"src":"http","src":"http"},{"src":"http","src":"http"}]< /code> 使用 json_encode。但是我的要求是这样的 [["http","http"],["http","http"]]

//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"]]

newbie here, my code below outputs like this [{"src":"http","src":"http"},{"src":"http","src":"http"}] using json_encode. however my requirement would be like this [["http","http"],["http","http"]]

//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 "]";
}
?>

my json_encode output is like this

[{"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"}]

how do i output it this way

[["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"]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-11-25 04:52:01

一种简单的方法是将 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;
}
?>

One simple way would be to save the json_encode result to a variable, and then str_replace the { and } characters.

So your code would become:

//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;
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文