0

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..

6
  • What's the desired output? Do you want to convert that array to something more structured? Commented Jul 23, 2013 at 11:54
  • I think he's rather trying to write less code. Use nested loops for it. Commented Jul 23, 2013 at 11:55
  • @PAM yes, you understood me in correct way. I am trying to make code shorter than 100s of arrays Commented Jul 23, 2013 at 11:55
  • @Lee trying to make code shorter than 100 arrays Commented Jul 23, 2013 at 11:56
  • But why? what is the input, and what is the desired output? Commented Jul 23, 2013 at 11:57

2 Answers 2

1

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"
    }
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Your code is working fine too but can you please guide me for my confusion. Please refer my comment on Prashanth Bendra's answer
@pixelngrain Looking, sorry, was busy
@pixelngrain I looked at your conversation with Prashanth Bendra. Can you update your question, specifying, what output do you want to get? Except for that, generated by my code. Currently, it's hard to understand, what you want.
Okay give me some time I will do
@pixelngrain To achieve this, you need to set static values to the array. Answer updated.
|
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");          
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.