PHP - 分裂数组时无限循环

时间:2014-12-22 07:52:45

标签: php arrays

我将一个数组拆分为五个不同的数组,其中数组块中的所有第一个元素都转到数组$ first,第二个转换为$ second,依此类推,这里是我的完整代码。请帮帮我。

<?php
$input = array("1", "2", "3", "4", "5", "10", "20", "30", "40", "50");

      $chunks = array_chunk($input, 5);

        $count = sizeof($chunks);
        $first = array();
        $second = array();
        $third = array();
        $fourth = array();
        $fifth = array();


       for($i=0;$i<$count;$i++) 
      {
        $new = $chunks[$i];
        for($j=0;$j<5;$j++)
        {
          if($j=0)
          {
          $new[$j] = $first[$i]; 
          }
          elseif($j=1)
          {
          $new[$j] = $second[$i]; 
          }
          elseif($j=2)
          {
          $new[$j] = $third[$i]; 
          }

          elseif($j=3)
          {
          $new[$j] = $fourth[$i]; 
          }

          elseif($j=4)
          {
          $new[$j] = $fifth[$i]; 
          }
          else
          {
          echo "error";
          }

        }

      }

        echo "<pre>";
        print_r($first);
        print_r($second);
        print_r($third);
        print_r($fourth);
        print_r($fifth);
    ?>

我尝试了很多方法,但总是在无限页面中加载,甚至崩溃我的Firefox。我现在坐了一整天,我无法弄明白。

5 个答案:

答案 0 :(得分:2)

你正在检查

$j=0

应该是

$j==0

如果您使用$j = 0,则始终会先输入if

for ($j=0; $j<5; $j++)// $j value will be always 1 it will never increase. and $j < 5 will be always true that's why the loop is looping infinitely.  

答案 1 :(得分:0)

使用==条件语句而不是assign语句,如果在if条件中使用赋值运算符(=),它会发生什么,它总是返回true,所以你的循环将变为无限,你也必须分配$ first [$ i ] = $ new [$ j];

<?php
$input = array("1", "2", "3", "4", "5", "10", "20", "30", "40", "50");

$chunks = array_chunk($input, 5);

$count = sizeof($chunks);
$first = array();
$second = array();
$third = array();
$fourth = array();
$fifth = array();


for($i=0;$i<$count;$i++)
{
    $new = $chunks[$i];
    for($j=0;$j<5;$j++)
    {
        if($j==0)
        {
            //$new[$j] = $first[$i];
            $first[$i] = $new[$j];
        }
        elseif($j==1)
        {
            $second[$i] = $new[$j];
        }
        elseif($j==2)
        {
            $third[$i] = $new[$j];
        }

        elseif($j==3)
        {
            $fourth[$i] = $new[$j];
        }

        elseif($j==4)
        {
            $fifth[$i] = $new[$j];
        }
        else
        {
            echo "error";
        }

    }

}

echo "<pre>";
print_r($first);
print_r($second);
print_r($third);
print_r($fourth);
print_r($fifth);

&GT;

答案 2 :(得分:0)

问题在于,您不会检查j是否等于某个数字,而是将j分配给某个数字。

将所有j=1//and the others替换为j == 1//and others

答案 3 :(得分:0)

如果条件用于checking,但您正在执行任务:

因此请使用==代替=

我不知道你为什么要这样做:

 $new[$j] = $first[$i]; // where $first array is blank array 

这样做:

$first[$i] =$new[$j];// to assign the content of new to $first;

答案 4 :(得分:0)

从阅读你的问题开始,我假设你正试图解决这个问题:

["1", "2", "3", "4", "5", "10", "20", "30", "40", "50"]

进入这个:

[["1", "2", "3", "4", "5"], ["10", "20", "30", "40", "50"]]

然后到此:

$first = ["1", "10"];
$second = ["2", "20"];
$third = ["3", "30"];
$fourth = ["4", "40"];
$fifth = ["5", "50"];

我们走吧:

<?php
$input = array("1", "2", "3", "4", "5", "10", "20", "30", "40", "50");

$chunks = array_chunk($input, 5);

// See below on using foreach
// $count = sizeof($chunks);
$first = array();
$second = array();
$third = array();
$fourth = array();
$fifth = array();

// You may consider the foreach() construct:
//for($i=0;$i<$count;$i++){
foreach($chunks as $index => $new){
    // I've updated the following to work, but I think you can do this easier. See below.

    for($j=0;$j<5;$j++){
        // You may consider the switch syntax:
        switch($j){
            case 0:
                $first[$index] = $new[$j]; 
                break;
            case 1:
                $second[$index] = $new[$j];
                break;
            case 2:
                $third[$index] = $new[$j];
                break;
            case 3:
                $fourth[$index] = $new[$j];
                break;
            case 4:
                $fifth[$index] = $new[$j];
                break;
            default:
                echo "error";
                break;
        }
    }

    // I appreciate what you're doing, but if you're using hard variable names, you can:
    $first[] = $new[0];
    $second[] = $new[1];
    $third[] = $new[2];
    $fourth[] = $new[3];
    $fifth[] = $new[4];
}

echo "<pre>";
print_r($first);
print_r($second);
print_r($third);
print_r($fourth);
print_r($fifth);
?>

但是,我会这样做:

<?php
$input = array("1", "2", "3", "4", "5", "10", "20", "30", "40", "50");

$chunks = array_chunk($input, 5);

$places = array(array(), array(), array(), array(), array());

// Loop through the chunks
foreach($chunks as $chunk){
    // Use $key as the index number as it is the "place" in the chunk
    foreach($chunk as $key => $value){
        // The $array[] = syntax just adds one to the end of the array.
        // Since we're going in order anyways, we can trust this instead of specifying an index
        $places[$key][] = $value;
    }
}

print_r($places);

// And if you want: $first = $places[0]; $second = $places[1]; etc etc
?>
相关问题