PHP assign one array value to another array - php

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

Related

FOREACH in LINE BOT FLEX

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,

How to Group a multidimensional array

How can I group the array based Name Menu Category and Name Menu ? Is there any native php functions are available to do this? I 've searched on google but not yet well understood
(int) 0 => array(
'MenuCategory' => array(
'name' => 'Products'
),
'Menu' => array(
'name' => 'A',
'url' => 'a'
),
'Sub' => array(
'name' => 'A1',
'url' => 'a1'
)
),
(int) 1 => array(
'MenuCategory' => array(
'name' => 'Products'
),
'Menu' => array(
'name' => 'A',
'url' => 'a'
),
'Sub' => array(
'name' => 'A2',
'url' => 'a2'
)
),
(int) 2 => array(
'MenuCategory' => array(
'name' => 'Products'
),
'Menu' => array(
'name' => 'B',
'url' => 'b'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 3 => array(
'MenuCategory' => array(
'name' => 'Data'
),
'Menu' => array(
'name' => 'A',
'url' => 'a'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 4 => array(
'MenuCategory' => array(
'name' => 'Data'
),
'Menu' => array(
'name' => 'B',
'url' => 'b'
),
'Sub' => array(
'name' => 'B1',
'url' => 'b1'
)
),
(int) 5 => array(
'MenuCategory' => array(
'name' => 'Data'
),
'Menu' => array(
'name' => 'C',
'url' => 'c'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 6 => array(
'MenuCategory' => array(
'name' => 'Report'
),
'Menu' => array(
'name' => 'A',
'url' => 'a'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 7 => array(
'MenuCategory' => array(
'name' => 'Report'
),
'Menu' => array(
'name' => 'B',
'url' => 'b'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 8 => array(
'MenuCategory' => array(
'name' => 'Report'
),
'Menu' => array(
'name' => 'C',
'url' => 'c'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
This is a result that will be issued later
Products
A
A1
A2
B
Data
A
B
B1
C
Report
A
B
C
if I assume your array variable name is $menu, try this code.
$new = array();
array_map(function($a) use(&$new){
if($a['Sub']['name'])
$new[$a['MenuCategory']['name']][$a['Menu']['name']][$a['Sub']['name']] = $a['Sub']['name'];
else
$new[$a['MenuCategory']['name']][$a['Menu']['name']] = $a['Menu']['name'];
}, $menu);
print_r($new);
and will output
Array
(
[Products] => Array
(
[A] => Array
(
[A1] => A1
[A2] => A2
)
[B] => B
)
[Data] => Array
(
[A] => A
[B] => Array
(
[B1] => B1
)
[C] => C
)
[Report] => Array
(
[A] => A
[B] => B
[C] => C
)
)
Hope this help.
Group them by level first
use foreach to sort them example
foreach ($input_arr as $key => &$entry) {
$level_arr[$entry['level']][$key] = $entry;
}

Pushing array variable from inside array variable which has been created dynamically funtion

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.

CakePHP 2.4.0 Recusion Inside of Contain in Paginator->settings Not Working

I'm having an issue with Paginator->settings. For some reason, recursive is not working. Any ideas? I've tried setting $this->Cheese->recursive = 2 and haven't had any luck.
$this->Cheese->recursive = 2;
$this->Paginator->settings = array(
'CheeseCheckin' => array(
'fields' => array('id','created','comment','location','rating','short_url_hash','foursquare_id'),
'conditions'=>array(
'CheeseCheckin.cheese_id' => $id
),
'contain' => array(
'Cheese'=>array(
'fields' => array('id','name','created','modified','attachment_id'),
'Attachment' => array(
'fields' => array('id','name','ext','path')
)
),
'CheeseProducer' => array(
'fields' => array('name','created', 'id','attachment_id')
),
'User' => array(
'fields' => $this->userFields,
'Attachment' => array(
'fields'=>array('id','name','ext','path')
),
'StateRegion' => array(
'fields'=>array('name','code')
),
'Country' => array(
'fields'=>array('name','code')
)
),
'Place' => array(
'fields'=>array('id','name')
),
'UserAttachment' => array(
'fields' => array('id','ext','name','path')
)
),
'page' => $page,
'order' => array('CheeseCheckin.created' => 'DESC'),
'limit' => $limit,
'recursive' => 2
)
);
debug($this->Paginator->paginate('CheeseCheckin'));
The results look like:
array(
(int) 0 => array(
'CheeseCheckin' => array(
'id' => '62',
'created' => '2013-09-06 13:34:44',
'comment' => 'This is a test',
'location' => null,
'rating' => '4',
'short_url_hash' => '18ARMkq',
'foursquare_id' => '40d77680f964a5205d011fe3'
),
'UserAttachment' => array(
'id' => null,
'ext' => null,
'name' => null,
'path' => null
),
'User' => array(
'id' => '1',
'username' => '...',
'name' => 'Rob',
'profile_image_url' => '...',
'active' => '1',
'email' => 'robksawyer#gmail.com',
'private' => false,
'attachment_id' => '616',
'country_id' => '228',
'state_region_id' => '48'
),
'Cheese' => array(
'id' => '379',
'name' => 'Party In a Jar',
'created' => '2012-10-05 16:57:04',
'modified' => '2013-09-13 02:12:27',
'attachment_id' => '460'
),
'CheeseProducer' => array(
'name' => 'Alsea Acre Goat Cheese',
'created' => '2012-10-05 16:50:51',
'id' => '43',
'attachment_id' => null
),
'Place' => array(
'id' => null,
'name' => null
)
)
)
Fixed the issue. I just had to add user_id, attachment_id, etc. to the fields array.

rearrange multidimensional array in cakephp 2.2

I have this multidimensional assoc $modules array in cakephp 2.2.3:
array(
'type1' => array(
(int) 0 => array(
'name' => 'name16',
'type' => '1',
'category' => 'categoryC',
'zone' => 'zone1'
),
(int) 1 => array(
'name' => 'name17',
'type' => '1',
'category' => 'categoryB',
'zone' => 'zone1'
),
(int) 2 => array(
'name' => 'name18',
'type' => '1',
'category' => 'categoryA',
'zone' => 'zone3'
),
(int) 3 => array(
'name' => 'name19',
'type' => '1',
'category' => 'categoryC',
'zone' => 'zone3'
),
(int) 4 => array(
'name' => 'name22',
'type' => '1',
'category' => 'categoryA',
'zone' => 'zone2'
)
),
'type2' => array(
(int) 0 => array(
'name' => 'name1',
'type' => '2',
'category' => 'categoryB',
'zone' => 'zone2'
),
(int) 1 => array(
'name' => 'name2',
'type' => '2',
'category' => 'categoryB',
'zone' => 'zone2'
),
(int) 2 => array(
'name' => 'name3',
'type' => '2',
'category' => 'categoryA',
'zone' => 'zone1'
),
(int) 3 => array(
'name' => 'name4',
'type' => '2',
'category' => 'categoryC',
'zone' => 'zone3'
),
(int) 4 => array(
'name' => 'name5',
'type' => '2',
'category' => 'categoryA',
'zone' => 'zone3'
)
)
)
and I want to rearrange it like
array(
'zone1' => array(
'type1' => array(
(int) 0 => array(
'name' => 'name17',
'type' => '1',
'category' => 'categoryB'
),
(int) 1 => array(
'name' => 'name16',
'type' => '1',
'category' => 'categoryC'
)
),
'type2' => array(
(int) 0 => array(
'name' => 'name3',
'type' => '2',
'category' => 'categoryA'
)
)
),
'zone2' => array(
'type1' => array(
(int) 0 => array(
'name' => 'name22',
'type' => '1',
'category' => 'categoryA'
)
),
'type2' => array(
(int) 0 => array(
'name' => 'name1',
'type' => '2',
'category' => 'categoryB'
),
(int) 1 => array(
'name' => 'name2',
'type' => '2',
'category' => 'categoryB'
)
)
),
'zone3' => array(
'type1' => array(
(int) 0 => array(
'name' => 'name18',
'type' => '1',
'category' => 'categoryA'
),
(int) 0 => array(
'name' => 'name19',
'type' => '1',
'category' => 'categoryC'
)
),
'type2' => array(
(int) 0 => array(
'name' => 'name5',
'type' => '2',
'category' => 'categoryA'
),
(int) 1 => array(
'name' => 'name4',
'type' => '2',
'category' => 'categoryC'
)
)
)
)
where 'category' should be sorted alphabetically within each parent 'type'
I tried with
$modules = Hash::combine($modules, '{n}', '{n}.{n}', '{s}.{s}.{n}.zone');
but with no success. I guess I'm not using Hash::combine as I should...
Thanks, Marius
Just use a foreach inside a foreach to walk through your array and construct a new one based on your preference.

Categories