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'];
}
Related
can anyone help me with this problem?
I had a LINE Messenger BOT, and I want to implement foreach() loop to show a list from the database I have. but I struggle with the code. here is what I wrote
function indexflex($title,$label,$text){
$data = $this->Dbs->getdata_kamusengineer()->result();
foreach ($data as $d) {
$dariindo=$d->dariindo;
$bajep=$d->bajep;
$arraynumber= 0;
$itemindeks=array (
'type' => 'box',
'layout' => 'vertical',
'contents' => array (
$arraynumber++ => array (
'type' => 'box',
'layout' => 'horizontal',
'contents' => array (
0 => array (
'type' => 'text',
'text' => $dariindo,
'align' => 'center',
'wrap' => true,
'gravity' => 'center',
'contents' => array (
),
),
1 => array (
'type' => 'separator',
'margin' => 'sm',
'color' => '#000000FF',
),
2 => array (
'type' => 'text',
'text' => $bajep,
'align' => 'center',
'align' => 'center',
'wrap' => true,
'gravity' => 'center',
'contents' =>
array (
),
),
),
),
),
);
}
$item = array (
'type' => 'bubble',
'direction' => 'ltr',
'header' => array (
'type' => 'box',
'layout' => 'vertical',
'contents' => array (
0 => array (
'type' => 'text',
'text' => $title,
'weight' => 'bold',
'align' => 'center',
'wrap' => true,
'contents' =>
array (
),
),
),
),
'body' => $itemindeks,
'footer' => array (
'type' => 'box',
'layout' => 'horizontal',
'contents' => array (
0 => array (
'type' => 'button',
'action' => array (
'type' => 'message',
'label' => $label,
'text' => $text,
),
'style' => 'primary',
),
),
),
);
return $item;
}
but the result not what i want, it just shows one of my data from many.
image of the result
can anyone help me what's wrong with this code?
Thanks,
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
)
array (
0 =>
array (
'label' => '1',
'index' => 1,
'product_attributes' =>
array (
0 =>
array (
'type' => 'product',
'id' => 1,
'label' => 'Size',
'placeholder' => 'Select Size',
'description' => '',
'defaultValue' =>
array (
'text' => 'Size32',
'price' => '22',
'isSelected' => false,
),
'choices' =>
array (
0 =>
array (
'text' => 'Size30',
'price' => '20',
'isSelected' => 'true',
),
1 =>
array (
'text' => 'Size32',
'price' => '22',
'isSelected' => false,
),
),
'conditionalLogic' => '',
),
1 =>
array (
'type' => 'product',
'id' => 2,
'label' => 'Color',
'placeholder' => 'Select Color',
'description' => 'DEsc',
'defaultValue' =>
array (
'text' => 'Black',
'price' => '5',
'isSelected' => 'false',
),
'choices' =>
array (
0 =>
array (
'text' => 'Black',
'price' => '5',
'isSelected' => 'false',
),
1 =>
array (
'text' => 'Green',
'price' => '6',
'isSelected' => 'false',
),
2 =>
array (
'text' => 'Blue',
'price' => '4',
'isSelected' => 'true',
),
3 =>
array (
'text' => 'White',
'price' => '1',
'isSelected' => 'false',
),
),
'conditionalLogic' => '',
),
2 =>
array (
'type' => 'product',
'id' => 3,
'label' => 'Fit',
'placeholder' => 'Select Fit',
'description' => 'Select Fit',
'defaultValue' =>
array (
),
'choices' =>
array (
0 =>
array (
'text' => 'Slim',
'price' => '2',
'isSelected' => false,
),
1 =>
array (
'text' => 'Regular',
'price' => '3',
'isSelected' => false,
),
2 =>
array (
'text' => 'Casual',
'price' => '5',
'isSelected' => false,
),
),
'conditionalLogic' => '',
),
),
'total_product_cost' => '$27.00',
'total_product_price' => '27.00',
'product_id' => '36',
),
1 =>
array (
'label' => 'label21',
'total_product_cost' => '$27.00',
'total_product_price' => '27.00',
'index' => 3,
'product_id' => '36',
'product_attributes' =>
array (
0 =>
array (
'type' => 'product',
'id' => 1,
'label' => 'Size',
'placeholder' => 'Select Size',
'description' => '',
'defaultValue' =>
array (
'text' => 'Size32',
'price' => '22',
'isSelected' => false,
),
'choices' =>
array (
0 =>
array (
'text' => 'Size30',
'price' => '20',
'isSelected' => 'true',
),
1 =>
array (
'text' => 'Size32',
'price' => '22',
'isSelected' => false,
),
),
'conditionalLogic' => '',
'conditionalLogic2' =>
array (
'actionType' => 'show',
'logicType' => 'all',
'checkbox' => true,
'rules' =>
array (
0 =>
array (
'fieldId' => 2,
'operator' => 'is',
'value' => 'Black',
),
),
),
),
1 =>
array (
'type' => 'product',
'id' => 2,
'label' => 'Color',
'placeholder' => 'Select Color',
'description' => 'DEsc',
'defaultValue' =>
array (
'text' => 'Black',
'price' => '5',
'isSelected' => 'false',
),
'choices' =>
array (
0 =>
array (
'text' => 'Black',
'price' => '5',
'isSelected' => 'false',
),
1 =>
array (
'text' => 'Green',
'price' => '6',
'isSelected' => 'false',
),
2 =>
array (
'text' => 'Blue',
'price' => '4',
'isSelected' => 'true',
),
3 =>
array (
'text' => 'White',
'price' => '1',
'isSelected' => 'false',
),
),
'conditionalLogic' => '',
),
2 =>
array (
'type' => 'product',
'id' => 3,
'label' => 'Fit',
'placeholder' => 'Select Fit',
'description' => 'Select Fit',
'defaultValue' =>
array (
),
'choices' =>
array (
0 =>
array (
'text' => 'Slim',
'price' => '2',
'isSelected' => false,
),
1 =>
array (
'text' => 'Regular',
'price' => '3',
'isSelected' => false,
),
2 =>
array (
'text' => 'Casual',
'price' => '5',
'isSelected' => false,
),
),
'conditionalLogic' => '',
),
),
),
)
I have posted my array value. This values are dynamic. Here is two array conditionalLogic and conditionalLogic2 I want to assign those array key conditionalLogic2 exist and value should be assign to conditionalLogic. After assign conditionalLogic values into the conditionalLogic2 remove that key from my array list.
Check I have tried this way but not working -
// $data['values'] array I have posted above
foreach ($data['values'] as $products) {
foreach ($products['product_attributes'] as $product_choices) {
if (!empty($product_choices['conditionalLogic2']) && $product_choices['conditionalLogic'] == '') {
$product_choices['conditionalLogic'] = $product_choices['conditionalLogic2'];
unset($product_choices['conditionalLogic2']);
}
}
}
var_export($data['values']); exit;
Please help me and give me any solution how to replace the array value into another array.
What is the shortcut way to solve this problem?
It is usually better to avoid nested loops:
foreach ($data as &$datum) {
if (!isset($datum['product_attributes'])) {
continue;
}
$datum['product_attributes'] = array_map(function ($productAttribute) {
if (
!empty($productAttribute['conditionalLogic2'])
&& empty($productAttribute['conditionalLogic'])
) {
$productAttribute['conditionalLogic'] = $productAttribute['conditionalLogic2'];
unset($productAttribute['conditionalLogic2']);
}
return $productAttribute;
}, $datum['product_attributes']);
}
Here I used array_map() function to assign new arrays directly.
Here is working example.
Your code is almost right, but you're acting a local variables in foreach loops. You should bind them to original array items as follows:
foreach ($data['values'] as & $products) {
foreach ($products['product_attributes'] as & $product_choices) {
if (!empty($product_choices['conditionalLogic2']) && $product_choices['conditionalLogic'] == '') {
$product_choices['conditionalLogic'] = $product_choices['conditionalLogic2'];
unset($product_choices['conditionalLogic2']);
}
}
}
unset($products);
unset($product_choices);
If there is the end of current function scope, both unset may be omitted. But you may remove references from an array explicitly to avoid undesired affects in some code below in the same scope. I.e. $products = 10; somewhere below crushes your last branch of an array.
#chinu You are actually assigning or changing the local variables where scope is limited within for loop only so in this case, we should use pass by reference.
See the following code, it will work:
foreach ($data['values'] as &$products) {
foreach ($products['product_attributes'] as &$product_choices) {
if (!empty($product_choices['conditionalLogic2']) && $product_choices['conditionalLogic'] == '') {
$product_choices['conditionalLogic'] = $product_choices['conditionalLogic
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.
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.