数组没有像我期待的那样填满

时间:2013-08-15 20:36:21

标签: php arrays

我有一个应该接受数组数组的函数,并将其拆分为每个包含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. 之前重置了数组
  3. 我在主函数和辅助函数中都在递增数组。
  4. 这是正确的代码:

    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;
    }                  
    

1 个答案:

答案 0 :(得分:0)

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

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