0

好的,所以数组是我的弱点,我正在为此苦苦挣扎。这可能也很简单。基本上,我有一个数组

代码:

    Array
    (
[data] => Array
    (
        [0] => Array
            (
                [name] => getalbum
                [fql_result_set] => Array
                    (
                        [0] => Array
                            (
                                [aid] => 187046464639937_101906
                                [name] => photo tab
                                [cover_pid] => 187046464639937_1661168
                            )

                        [1] => Array
                            (
                                [aid] => 187046464639937_99627
                                [name] => Cover Photos
                                [cover_pid] => 187046464639937_1661054
                            )

                    )

            )

        [1] => Array
            (
                [name] => getcover
                [fql_result_set] => Array
                    (
                        [0] => Array
                            (
                                [src] => http://photos-g.ak.fbcdn.net/hphotos-ak-prn1/526499_403098366368078_187046464639937_1661168_217706037_s.jpg
                            )

                        [1] => Array
                            (
                                [src] => http://photos-b.ak.fbcdn.net/hphotos-ak-ash3/562470_403049039706344_187046464639937_1661054_361917190_s.jpg
                            )

                    )

            )

    )

    )

我正在使用 foreach 通过 PHP 代码调用数组:

foreach($fql_query_obj['data'] as $albums){
    foreach($albums['fql_result_set'] as $album){
        //print_r($album);
        if (   ($album['name'] != "Wall Photos")
            && ($album['name'] != "Cover Photos") 
            && ($album['name'] != "photo tab"))
        {
             echo $album['name'].' :: '.$album['aid'];
             echo '<img src="'.$album['src'].'" /><br />';
        }    
     }
} 

现在它似乎通过 getalbum 数组然后是 getcover。我该怎么做才能将它们合并在一起?

4

2 回答 2

0

您如何获取数据,某种数据库?也许您可以在获取该数据时合并它?

无论如何,如果您的数据总是以这种方式排序(匹配数组,一个带有数据的第一部分,第二个带有附加数据),那么这样的事情怎么样?

foreach($fql_query_obj['data'][0]['fql_result_set'] as $key_album => $album){
    //print_r($album);
    if (   ($album['name'] != "Wall Photos")
        && ($album['name'] != "Cover Photos") 
        && ($album['name'] != "photo tab"))
    {
         echo $album['name'].' :: '.$album['aid'];
         echo '<img src="'.$fql_query_obj['data'][1]['fql_result_set'][$key_album]['src'].'" /><br />';
    }    
 }
于 2012-04-09T23:03:41.423 回答
0

您似乎正在尝试迭代查询(getalbumgetcover)。相反,您可能希望迭代专辑并将封面和专辑数据视为一个统一的东西。我会尝试重组数据以使其更易于使用:

list($albumsQuery, $coversQuery) = $fql_query_obj['data'];
$albums = array();

foreach ($albumsQuery['fql_result_set'] as $album) {
    $albums[] = $album;
}

for ($i = 0, $numAlbums = count($albums); $i < $numAlbums; ++$i) {
    $albums[$i] = array_merge($albums[$i], $coversQuery['fql_result_set'][$i]);
}

foreach ($albums as $album) {
    // Here, $album contains data from both arrays
}

请注意,这假设没有冲突的键(您的示例数据中没有),并且还有很多其他方法可以做到这一点。

于 2012-04-09T23:23:27.453 回答