0

我有一个函数应该接收一个数组数组,并将其拆分为一个数组,每个数组包含 100 个项目。问题是当我执行它时,在这种情况下,将 1137 个项目传递给它,返回数组有 12 个数组,除了最后一个之外都是空的。

这是代码:

$names_to_fetch_from_twitter = array (
    array('screen_name' => 'acme', 'profiles_rownum' => 1, 'screen_name_rownum' => 1),
    array('screen_name' => 'acme1', 'profiles_rownum' => 2, 'screen_name_rownum' => 2),
    array('screen_name' => 'acme2', 'profiles_rownum' => 3, 'screen_name_rownum' => 3),
    array('screen_name' => 'acme3', 'profiles_rownum' => 4, 'screen_name_rownum' => 4),
    array('screen_name' => 'acme4', 'profiles_rownum' => 5, 'screen_name_rownum' => 5)
        );

$names_to_fetch_from_twitter_organized = split_into_blokcs_of_3 ($names_to_fetch_from_twitter);

echo '<pre>';
print_r($names_to_fetch_from_twitter_organized);
echo '</pre>';

function split_into_blokcs_of_3 ($names_to_fetch_from_twitter)
{
    $names_to_fetch_from_twitter_organized = array();
    $count = 1;
    $screen_names = array();
    $profiles_row_nums = array();
    $screen_name_rownums = array();
    foreach ($names_to_fetch_from_twitter as $name_to_fetch_from_twitter) {
        $screen_names[] = $name_to_fetch_from_twitter['screen_name'];
        $profiles_row_nums[] = $name_to_fetch_from_twitter['profiles_rownum'];
        $screen_name_rownums[] = $name_to_fetch_from_twitter['screen_name_rownum'];
        $count++;
        if ($count > 3) {
            $count = 1;
            $screen_names = array();
            $profiles_row_nums = array();
            $screen_name_rownums = array();            
            $names_to_fetch_from_twitter_organized[] = 
                    package_into_array($screen_names,$profiles_row_nums,$screen_name_rownums,$names_to_fetch_from_twitter_organized);
        }
    }
    if ($count > 1) {
        $names_to_fetch_from_twitter_organized[] = 
                package_into_array($screen_names,$profiles_row_nums,$screen_name_rownums,$names_to_fetch_from_twitter_organized);
    }
    return $names_to_fetch_from_twitter_organized;
}

function package_into_array($screen_names,$profiles_row_nums,$screen_name_rownums,$names_to_fetch_from_twitter_organized) 
{
    $names_to_fetch_from_twitter_organized[] = array (
        'screen_names' => implode (',',$screen_names),
        'profiles_row_nums' => $profiles_row_nums,
        'screen_name_rownums' => $screen_name_rownums
    );    
    return $names_to_fetch_from_twitter_organized;
}

这是输出:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [screen_names] => 
                    [profiles_row_nums] => Array
                        (
                        )
                    [screen_name_rownums] => Array
                        (
                        )
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [screen_names] => 
                            [profiles_row_nums] => Array
                                (
                                )
                            [screen_name_rownums] => Array
                                (
                                )
                        )
                )
            [1] => Array
                (
                    [screen_names] => acme3,acme4
                    [profiles_row_nums] => Array
                        (
                            [0] => 4
                            [1] => 5
                        )
                    [screen_name_rownums] => Array
                        (
                            [0] => 4
                            [1] => 5
                        )
                )
        )
)

当我期待以下内容时:

Array
(
    [0] => Array
        (
            [screen_names] => acme1,acme2,acme3
            [profiles_row_nums] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )    
            [screen_name_rownums] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3   
                )
        )    
    [1] => Array
        (
            [screen_names] => acme4,acme5
            [profiles_row_nums] => Array
                (
                    [0] => 1
                    [1] => 2
                )    
            [screen_name_rownums] => Array
                (
                    [0] => 1
                    [1] => 2
                ) 
        )      
)                                      

有任何想法吗?

解决方案:

我有几个错误:

  1. 在将数组包含在主数组中之前,我正在重置数组
  2. 我在主函数和辅助函数中都增加了数组。

这是正确的代码:

function split_into_blokcs_of_3 ($names_to_fetch_from_twitter)
{
    $names_to_fetch_from_twitter_organized = array();
    $count = 1;
    $screen_names = array();
    $profiles_row_nums = array();
    $screen_name_rownums = array();
    foreach ($names_to_fetch_from_twitter as $name_to_fetch_from_twitter) {
        $screen_names[] = $name_to_fetch_from_twitter['screen_name'];
        $profiles_row_nums[] = $name_to_fetch_from_twitter['profiles_rownum'];
        $screen_name_rownums[] = $name_to_fetch_from_twitter['screen_name_rownum'];
        $count++;
        if ($count > 3) {   
            $names_to_fetch_from_twitter_organized = 
                    package_into_array($screen_names,$profiles_row_nums,$screen_name_rownums,$names_to_fetch_from_twitter_organized);
            $count = 1;
            $screen_names = array();
            $profiles_row_nums = array();
            $screen_name_rownums = array();                     
        }
    }
    if ($count > 1) {
        $names_to_fetch_from_twitter_organized = 
                package_into_array($screen_names,$profiles_row_nums,$screen_name_rownums,$names_to_fetch_from_twitter_organized);
    }
    return $names_to_fetch_from_twitter_organized;
}                  
4

1 回答 1

0

我想您可能会假设在 if($count > 3) 块内创建了一个新数组。

您可以尝试在那里添加 new array() 而不是仅仅添加数组,但这应该会覆盖之前的内容。

于 2013-08-15T20:43:49.900 回答