Reputation: 8348
I am not so familiar but sure there should be some way to shorten my code. I am having multi-dimention array as below.
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'
),
),
);
Now I have total 10 items (please refer id) and each item has 10 fields (in reference code only 3). So if I would write code for each field it will become massive around 100 array so I am looking for some way where I can repeat loop for each item.
I hope I explained properly..
Upvotes: 0
Views: 128
Reputation: 23719
You need to find the patterns in your data and create the code, generating these patterns, instead of writing everything by hand:
<?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);
Prints:
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"
}
}
}
Upvotes: 1
Reputation: 838
$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");
}
}
Upvotes: 0