【问题标题】:Creating arrays with for loops使用 for 循环创建数组
【发布时间】:2014-03-12 13:12:03
【问题描述】:

我正在尝试创建一个名为“eggs”的多维数组,并使用for 循环向其中添加 3 个数组。然后,我试图将他们的 ID 分配给他们的数组编号。我的代码如下:

error_reporting(E_ALL);

$eggCount = 3;

for($i = 1; $i <= $eggCount; $i++) {
    create("eggs", array($i => array("ID" => $i)));
}

function create($arrName, Array $arrKeys) {
    $lName  = strtolower($arrName);
    $$lName = array();
    foreach($arrKeys as $key => $value) {
        $$lName[$key] = $value;
    }
    for($i = 1; $i <= $GLOBALS['eggCount']; $i++) {
        echo "Egg $i's ID: " . $eggs[$i]['ID'];
    }
}

它输出以下内容:

Notice: Undefined offset: 1 on line 16
Egg 1's ID: 
Notice: Undefined offset: 2 on line 16
Egg 2's ID: 
Notice: Undefined offset: 3 on line 16
Egg 3's ID: 
Notice: Undefined offset: 1 on line 16
Egg 1's ID: 
Notice: Undefined offset: 2 on line 16
Egg 2's ID: 
Notice: Undefined offset: 3 on line 16
Egg 3's ID: 
Notice: Undefined offset: 1 on line 16
Egg 1's ID: 
Notice: Undefined offset: 2 on line 16
Egg 2's ID: 
Notice: Undefined offset: 3 on line 16
Egg 3's ID:

我希望它输出这个:

Egg 1's ID: 1
Egg 2's ID: 2
Egg 3's ID: 3

编辑: 由于上面的示例相当令人困惑和奇怪,这就是我真正想要做的 - 为服务器创建配置文件 -

function CreateConfig($name, Array $arr) {
    $lName    = strtolower($name);
    $lolswag  = fopen("Config/$name.php", "a");
    foreach($arr as $key => $value) {
        $contents = '<?php
        ' . $$lName . ' = array(
        ' . $key . ' => ' . $value . ',' .
        ');' .
        '?>';
    }
    fwrite($lolswag, $contents);
    fclose($lolswag);
}

    $serverCount  = Base\Console::GetInput("Number of game servers: ");
$serverHandle = array();

for($i = 1; $i <= $serverCount; $i++) {
    $serverHandle[$i] = array("Address" => Base\Console::GetInput("Server $i Address: "), "Name" => Base\Console::GetInput("Server $i Name: "), "MaxClients" => Base\Console::GetInput("Server $i Maximum Clients: "), Base\Console::GetInput("Server $i Port: "));
    CreateConfig("Servers", array($i => $serverHandle[$i]));
}

【问题讨论】:

  • 你还没有声明数组$eggs
  • @krishna 我在for 循环中使用第一个参数eggs 调用了我的create() 函数。
  • 如果你使用可变变量,你几乎肯定做错了什么。
  • 变量变量有什么问题?自从我发现它们以来,它们对我非常有用。

标签: php arrays loops for-loop


【解决方案1】:

你有几个错误

用于创建$eggs变量

改一下

 $lName  = strtolower($arrName);

 $lName  = "$".strtolower($arrName);

那么你没有在任何地方明确声明$eggs,所以你必须使用$$lName而不是$eggs

【讨论】:

    【解决方案2】:

    您的代码看起来很奇怪,但要解决您的问题:

    for($i = 1; $i <= $GLOBALS['eggCount']; $i++) {
        echo "Egg $i's ID: " . $$lName[$i]['ID']; //Use the newly created array
    }
    

    【讨论】:

      【解决方案3】:

      我假设你想要这样的东西:

      $eggsCount = 3;
      $eggs = array();
      
      for($i = 0; $i < 3; $i++) {
          $eggs[$i] = create_egg(array('ID' => $i));
      }
      
      print_r($eggs);
      
      function create_egg($arrayKeys) {
          $outputArray = array();
          foreach($arrayKeys as $key => $value) {
              $outputArray[$key] = $value;
          }
          return $outputArray;
      }
      

      输出:

      Array
      (
          [0] => Array
          (
              [ID] => 0
          )
          [1] => Array
          (
              [ID] => 1
          )
          [2] => Array
          (
              [ID] => 2
          )
      )
      

      【讨论】:

        【解决方案4】:
        <?php
        error_reporting(E_ALL);
        
        $eggCount = 3;
        $mainaraay = array();
        for($i = 1; $i <= $eggCount; $i++) {
           $mainaraay[] = array("ID" => $i);
        }
        
        create("eggs", $mainaraay,$eggCount);
        function create($arrName,$arrKeys,$eggCount) {
        
            $lName  = strtolower($arrName);
        
            $i = 1;
            foreach($arrKeys as $key => $value) {
               echo "<br/>Egg $i's ID: " . $value['ID'];
               $i++;
            }
        
        }
        ?>
        

        试试这个........

        【讨论】:

          【解决方案5】:
              As per my understanding below code will help you.
          
              $eggCount = 3;
          
              for($i = 1; $i <= $eggCount; $i++) {
                       echo "Egg"." ".$i."'s ID: ".$i;
                      echo "<br/>";
                      $eggs['Egg'][$i]= array("ID" => $i);  
              }
          
          
            Your code is repeating the loop and $$lName is generating the error.
            Below is your formatted code :- 
            error_reporting(E_ALL);
          
            $eggCount = 3;
          
            for($i = 1; $i <= $eggCount; $i++) {
              create("eggs", array($i => array("ID" => $i)));
            }
          
            function create($arrName, Array $arrKeys) {
          
              foreach($arrKeys as $key => $value) {
                   echo "Egg ".$key."'s ID: " . $key;
                   echo "<br/>";
              }
           }
          

          【讨论】:

            【解决方案6】:
            error_reporting(E_ALL);    
            $eggCount = 3;
            
            for($i = 1; $i <= $eggCount; $i++) {
                create("eggs", array($i => array("ID" => $i)));
            }
            
            function create($arrName, Array $arrKeys) {
                $lName  = strtolower($arrName);
            
                foreach($arrKeys as $key => $value) {
                    $final[$lName][$key] = $value;
                    echo "Egg $key's ID: " .  $final[$lName][$key]['ID'];
                }
            

            【讨论】:

              猜你喜欢
              • 2015-05-25
              • 1970-01-01
              • 2013-04-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-05-08
              相关资源
              最近更新 更多