使用for循环创建数组

时间:2014-03-12 13:12:03

标签: php arrays loops for-loop

我正在尝试创建一个名为" 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]));
}

6 个答案:

答案 0 :(得分:0)

你有几个错误

用于创建$eggs变量

更改此

 $lName  = strtolower($arrName);

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

然后您尚未明确声明$eggs,因此您必须使用$$lName代替$eggs

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

我认为你想要这样的东西:

$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 :(得分:0)

<?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 :(得分:0)

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