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?
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
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
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.
Related
I have a problem to output subarray values properly. I use Codestar Framework to create options.
Here is my options code:
array(
'id' => 'rows',
'type' => 'group',
'title' => 'Rows',
'accordion_title_auto' => false,
'accordion_title_prefix' => 'Row',
'accordion_title_number' => true,
'fields' => array(
array(
'id' => 'columns',
'type' => 'group',
'title' => 'Columns',
'accordion_title_auto' => false,
'accordion_title_prefix' => 'Column',
'accordion_title_number' => true,
'fields' => array(
array(
'id' => 'column-color',
'type' => 'select',
'title' => 'Color',
'options' => array(
'red' => 'Red',
'blue' => 'Blue',
'green' => 'Green',
),
),
array(
'id' => 'column-price',
'type' => 'select',
'title' => 'Price',
'options' => array(
'low' => 'Low',
'medium' => ' Medium',
'high' => ' High',
),
),
),
),
),
),
I have done it so far and it works. But how to output color and price? I can't figure it out how to do it correctly.
$rows = $options['rows'];
foreach ( $rows as $id => $name ) {
echo '<div class="row-'. $id .'">';
foreach ( $name['columns'] as $id_col => $name_col ) {
echo '<div class="column-'. $id_col .'">';
echo'...';
echo '</div>';
}
echo '</div>';
}
<div class="row-0">
<div class="column-0">...</div>
<div class="column-1">...</div>
</div>
<div class="row-1">
<div class="column-0">...</div>
</div>
I want to output look like this:
<div class="row-0">
<div class="column-0 color-red">
Price - Low
</div>
<div class="column-1 color-green">
Price - Medium
</div>
</div>
</div>
<div class="row-1">
<div class="column-0 color-blue">
Price - High
</div>
</div>
Can anyone help me with this? I will be very grateful, thanks
UPDATE
This code displays all values:
foreach ( $rows as $id => $name ) {
echo '<div class="row-'. $id .'">';
foreach ( $name['columns'] as $id_col => $name_col ) {
echo '<div class="column-'. $id_col .' color-'. $name_col['column-color'] .'">';
echo $name_col['column-content'];
echo '</div>';
}
echo '</div>';
}
I just don't know how to make 'column-content' output not an option key string but a key value/label
You can try something like this:
$options = array(
'id' => 'rows',
'type' => 'group',
'title' => 'Rows',
'accordion_title_auto' => false,
'accordion_title_prefix' => 'Row',
'accordion_title_number' => true,
'fields' => array(
array(
'id' => 'columns',
'type' => 'group',
'title' => 'Columns',
'accordion_title_auto' => false,
'accordion_title_prefix' => 'Column',
'accordion_title_number' => true,
'fields' => array(
array(
'id' => 'column-color',
'type' => 'select',
'title' => 'Color',
'options' => array(
'red' => 'Red',
'blue' => 'Blue',
'green' => 'Green',
),
),
array(
'id' => 'column-price',
'type' => 'select',
'title' => 'Price',
'options' => array(
'low' => 'Low',
'medium' => ' Medium',
'high' => ' High',
),
),
),
),
),
);
$fields = $options['fields'];
print_r( $fields );
foreach ( $fields as $key => $field ) {
$color_options = $field['fields'][0]['options'];
$price_options = $field['fields'][1]['options'];
print_r( $price_options );
}
which will generate something like this:
Array
(
[0] => Array
(
[id] => columns
[type] => group
[title] => Columns
[accordion_title_auto] =>
[accordion_title_prefix] => Column
[accordion_title_number] => 1
[fields] => Array
(
[0] => Array
(
[id] => column-color
[type] => select
[title] => Color
[options] => Array
(
[red] => Red
[blue] => Blue
[green] => Green
)
)
[1] => Array
(
[id] => column-price
[type] => select
[title] => Price
[options] => Array
(
[low] => Low
[medium] => Medium
[high] => High
)
)
)
)
)
Array
(
[low] => Low
[medium] => Medium
[high] => High
)
I do not know if this is possible or not, I should ask rather than just be quiet, can json array result using loops? for example
i have loops script like this
$count = count($json);
if($count > 10){
$count = 10;
}
for($i=0; $i < $count; $i++) {
...
}
and then, i have a json array that only has 1 path, json is like this
array (
'type' => 'template',
'altText' => 'this is a carousel template',
'template' =>
array (
'type' => 'carousel',
'columns' =>
array (
0 =>
array (
'thumbnailImageUrl' => 'https://example.com/images/item1.jpg',
'imageBackgroundColor' => '#FFFFFF',
'title' => 'this is menu',
'text' => 'description',
'actions' =>
array (
0 =>
array (
'type' => 'postback',
'label' => 'Buy',
'data' => 'action=buy&itemid=111',
),
),
),
),
),
)
is there a way for my loops function to work on json array above? so for path ['template']['columns'] can generate 10 paths according to the number of loops I have, so the result is like this if the loops function just 2
array (
'type' => 'template',
'altText' => 'this is a carousel template',
'template' =>
array (
'type' => 'carousel',
'columns' =>
array (
0 =>
array (
'thumbnailImageUrl' => 'https://example.com/images/item1.jpg',
'imageBackgroundColor' => '#FFFFFF',
'title' => 'this is menu',
'text' => 'description',
'actions' =>
array (
0 =>
array (
'type' => 'postback',
'label' => 'Buy',
'data' => 'action=buy&itemid=111',
),
),
),
1 =>
array (
'thumbnailImageUrl' => 'https://example.com/images/item1.jpg',
'imageBackgroundColor' => '#FFFFFF',
'title' => 'this is menu',
'text' => 'description',
'actions' =>
array (
0 =>
array (
'type' => 'postback',
'label' => 'Buy',
'data' => 'action=buy&itemid=111',
),
),
),
),
),
)
thank you
I don't know what do you want. But this is my understanding about your question
$jsson = array (
'type' => 'template',
'altText' => 'this is a carousel template',
'template' =>
array (
'type' => 'carousel',
'columns' =>
array (
0 =>
array (
'thumbnailImageUrl' => 'https://example.com/images/item1.jpg',
'imageBackgroundColor' => '#FFFFFF',
'title' => 'this is menu',
'text' => 'description',
'actions' =>
array (
0 =>
array (
'type' => 'postback',
'label' => 'Buy',
'data' => 'action=buy&itemid=111',
),
),
),
),
),
);
for($i=1; $i < 2; $i++){
$jsson['template']['columns'][$i] = $jsson['template']['columns'];
}
I would like to have this array from
array(
'label'=> 'Guest Capacity',
'desc' => 'Total capacity of Guests',
'id' => 'vessel_guest_capacity',
'type' => 'select',
'options' => array (
1 => array ( 'label' => '1', 'value' => 1 ),
2 => array ( 'label' => '2', 'value' => 2 ),
3 => array ( 'label' => '3', 'value' => 3 ),
)
);
from this function, what is wrong with that?
array(
'label'=> 'Guest Capacity',
'desc' => 'Total capacity of Guests',
'id' => 'vessel_guest_capacity',
'type' => 'select',
'options' => function() { for($i = 1; $i <= 50; $i++){ if(!is_set($var)){ $var = array(); } $var[] = array ( 'label' => "$i", 'value' => $i ); } $this['options'] = $var; },
),
I tried with return but didn't have any luck.
hello I am using reusable custom metaboxes from https://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes..
these are my fields
array(
'label' => __('Setup Slider', 'crispy' ),
'desc' => __('create your slider image/ text using these repeatable options', 'crispy' ),
'id' => $prefix.'repeatable',
'type' => 'repeatable',
'sanitizer' => array(
'title' => 'sanitize_text_field',
'desc' => 'wp_kses_data'
),
'repeatable_fields' => array (
array(
'label' => __(' Slider Text alignment', 'crispy'),
'id' => 'alignment',
'type' => 'radio',
'options' => array (
'one' => array (
'label' => __('Left', 'crispy' ),
'value' => 'left'
),
'two' => array (
'label' => __('Center', 'crispy' ),
'value' => 'center'
),
'three' => array (
'label' => __('Right', 'crispy' ),
'value' => 'right'
)
)
),
array(
'label' => __('Background Image/pattern', 'crispy' ),
'id' => 'image',
'type' => 'image'
),
array(
'label' => __('Title', 'crispy' ),
'id' => 'title',
'type' => 'text'
),
array(
'label' => __('Description', 'crispy' ),
'id' => 'desc',
'type' => 'textarea'
),
)
),
My problem is i don't know how to store the fields value... can anyone resolve my problem!!..
$home_slider_alignment = get_post_meta( $post->ID, 'alignment', true);
i used this but doesn't help!!.. Those fields can be repeatable so the values are stored in array!!.. i don't know how to retrieve stored values from that array??..
Please help me!!.
It is working :)
enter code here
<?php
$projectgallery = get_post_meta($post->ID, 'arcadia_well_projectgallery', true); {
echo '<ul class="custom_repeatable">';
foreach ($projectgallery as $project) {
echo '<li>';
foreach($project as $i=>$value) {
echo $value;
}
echo '</li>';
}
echo '</ul>';
}
?>
I have multiple arrays:
$meta_boxes[] = array(
'id' => 'measurements',
'title' => 'Measurements',
'fields' => array(
array(
'name' => 'Length',
'id' => 'length',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Manufacturer Length',
'id' => 'manufacturer_length',
'type' => 'text',
'std' => ''
)
)
);
$meta_boxes[] = array(
'id' => 'colors',
'title' => 'Colors',
'fields' => array(
array(
'name' => 'exterior',
'id' => 'exterior',
'type' => 'text',
'std' => ''
etc...
How can I get for example, the value of the name element from fields array from $meta_boxes[] array with id = measurements?
Try something like this:
foreach ($meta_boxes as $meta_box) {
if($meta_box['id'] !== 'measurements') {
continue;
}
$output = $meta_box['fields'];
break;
}