-1

I want to dynamic create an array based on a number inside a multidimensional array

here is the code

$meta_box = array(  
'id' => 'my-meta-box',
'title' => 'Custom Input Fields',
'page' => 'page',
'context' => 'normal',
'priority' => 'high',
'fields' => array (
                  array( //this array must be created dynamic 
                      'name' => 'Textarea',
                      'desc' => 'Enter big text here',
                      'id' => 'textarea', //id is textarea + number
                      'type' => 'textarea',
                      'std' => 'Default value'
                  )
            )
);

I want the last array to be created dynamic by a number so if the number is 2 there must be 2 arrays in there with the same name,desc,type,str but a diffrent ID.

is this possible is somekind of way?

3
  • 2
    Have you tried creating the array using a loop? Commented Oct 19, 2011 at 13:18
  • can you give me an example how cause im really stuck Commented Oct 19, 2011 at 13:25
  • Show some code you have and tell us what you expect it to do. Commented Oct 19, 2011 at 13:27

3 Answers 3

7

Just add them dynamically by iterating over the number of ids:

$meta_box = array
(
    'id' => 'my-meta-box',
    'title' => 'Custom Input Fields',
    'page' => 'page',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array ()
);


$dynamicNumber = 2;
$idPrefix = 'textarea';
assert('$dynamicNumber > 0');
$dynamicIds = range(1, $dynamicNumber);

$fields = &$meta_box['fields'];
foreach($dynamicIds as $id)
{
    $fields[] = array( //this array must be created dynamic 
                      'name' => 'Textarea',
                      'desc' => 'Enter big text here',
                      'id' => sprintf('%s%d', $idPrefix, $id), //id is textarea + number
                      'type' => 'textarea',
                      'std' => 'Default value'
                  );
}
unset($fields);

Demo

Sign up to request clarification or add additional context in comments.

Comments

2

Here's a way to add each 'fields' sub array as a new array into the larger array

$meta_box = array(  
'id' => 'my-meta-box',
'title' => 'Custom Input Fields',
'page' => 'page',
'context' => 'normal',
'priority' => 'high');

$fields = array();

$numberOfArrays = 2;

for($i = 1; $i <= $numberOfArrays; $i++){
    $fields[$i] = array (
                  array( //this array must be created dynamic 
                      'name' => 'Textarea',
                      'desc' => 'Enter big text here',
                      'id' => 'textarea' . $i, //id is textarea + number
                      'type' => 'textarea',
                      'std' => 'Default value'
                  )
            );
}

$meta_box['fields'] = $fields;

echo '<pre>';
print_r($meta_box);
echo '</pre>';

You'll get an output like this in your browser:

Array
(
    [id] => my-meta-box
    [title] => Custom Input Fields
    [page] => page
    [context] => normal
    [priority] => high
    [fields] => Array
        (
            [1] => Array
                (
                            [name] => Textarea
                            [desc] => Enter big text here
                            [id] => textarea1
                            [type] => textarea
                            [std] => Default value
                )
        [2] => Array
                (
                            [name] => Textarea
                            [desc] => Enter big text here
                            [id] => textarea2
                            [type] => textarea
                            [std] => Default value
                )
        )
)

Demo

Comments

0

First you create the array $meta_box as follows:

$meta_box = array(  
  'id' => 'my-meta-box',
  'title' => 'Custom Input Fields',
  'page' => 'page',
  'context' => 'normal',
  'priority' => 'high',
  'fields' => array ()
);

Then you can add the 'dynamic' arrays as follows:

$number = 2;
for ($i = 1; $i <= $number; $i++) {
  $meta_box['fields'][] = array(
    'name' => 'Textarea',
    'desc' => 'Enter big text here',
    'id' => 'textarea_' . $i, //id is textarea + number
    'type' => 'textarea',
    'std' => 'Default value'
  );
}

This starts the numbering for the ids at 1 until $number.

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.