PHP循环生成数组

时间:2013-07-23 11:48:28

标签: php arrays loops

我不太熟悉但确定应该有一些方法来缩短我的代码。我有多维数组,如下所示。

return array(

    'save' => 'here is the save message',

    'options' => array(

        // section for item 1
        array(
            'name' => 'Item 1',
            'type' => 'text',
            'id' => 'item_1_type_1',
        ),

        array(
            'name' => 'Item 2',
            'type' => 'text',
            'id' => 'item_1_type_2',
        ),

        array(
            'name' => 'Item 3',
            'type' => 'text',
            'id' => 'item_1_type_3',
        ),


        // section for item 2
        array(
            'name' => 'Item 1',
            'type' => 'text',
            'id' => 'item_2_type_1',
        ),

        array(
            'name' => 'Item 2',
            'type' => 'text',
            'id' => 'item_2_type_2',
        ),

        array(
            'name' => 'Item 3',
            'type' => 'text',
            'id' => 'item_2_type_3',
        ),

        // here I also may add more fields aprart from loop
        // but that would be an array with the same format

        'submit' => array(
                    'name' => 'Save Options',
                    'id' => 'save_theme_options'
                ),
    ),

);

现在我总共有10个项目(请参阅id),每个项目有10个字段(参考代码只有3个)。因此,如果我为每个字段编写代码,它将变成大约100个数组,所以我正在寻找一些方法,我可以为每个项目重复循环。

我希望我能正确解释..

2 个答案:

答案 0 :(得分:1)

您需要在数据中找到模式并创建代码,生成这些模式,而不是手工编写所有内容:

<?php
$array = array(
    'save' => 'here is the save message',
    'options' => array(),
);

$n = 2;
$m = 3;
for ($i = 1; $i <= $n; ++$i)
{
    for ($j = 1; $j <= $m; ++$j)
    {
        $element = array(
            'name' => "Item $i",
            'type' => 'text',
            'id' => "item_" . $i . "_type_$j",
            );
        array_push($array['options'], $element);
    }
}

$array['options']['submit'] = array(
    'name' => 'Save Options',
    'id' => 'save_theme_options'
    );

var_dump($array);

打印:

array(2) {
  ["save"]=>
  string(24) "here is the save message"
  ["options"]=>
  array(7) {
    [0]=>
    array(3) {
      ["name"]=>
      string(6) "Item 1"
      ["type"]=>
      string(4) "text"
      ["id"]=>
      string(13) "item_1_type_1"
    }
    [1]=>
    array(3) {
      ["name"]=>
      string(6) "Item 1"
      ["type"]=>
      string(4) "text"
      ["id"]=>
      string(13) "item_1_type_2"
    }
    [2]=>
    array(3) {
      ["name"]=>
      string(6) "Item 1"
      ["type"]=>
      string(4) "text"
      ["id"]=>
      string(13) "item_1_type_3"
    }
    [3]=>
    array(3) {
      ["name"]=>
      string(6) "Item 2"
      ["type"]=>
      string(4) "text"
      ["id"]=>
      string(13) "item_2_type_1"
    }
    [4]=>
    array(3) {
      ["name"]=>
      string(6) "Item 2"
      ["type"]=>
      string(4) "text"
      ["id"]=>
      string(13) "item_2_type_2"
    }
    [5]=>
    array(3) {
      ["name"]=>
      string(6) "Item 2"
      ["type"]=>
      string(4) "text"
      ["id"]=>
      string(13) "item_2_type_3"
    }
    ["submit"]=>
    array(2) {
      ["name"]=>
      string(12) "Save Options"
      ["id"]=>
      string(18) "save_theme_options"
    }
  }
}

答案 1 :(得分:0)

$sections = "Section1,Section2";
$itemCount = 3; // how many items you need
$generated = array(); // generated array

foreach(explode(",",$sections) as $k/*for index number*/=>$v/*section name*/)
{
    for(int i = 0; i < $itemCount; $i++)
    {
        $generated[] = array("name" => "Item ".($i+1/*starts from 0*/), "type"=> "item", "id" => "$v_$k_type_$i");          
    }
}