Generate product combinations based on options - php

I'm trying to generate a product combination system based on 2 or more types of options where the final array should look like as displayed in the bellow example. I'm a bit new to this and I have no clue how to do it.
They array keys should start from 1000 and increment from there. The first part of the array contains info about option like option_id name etc and the second part contains some parts of the products in product_option_value.
Any advice or help would be helpful because i'm out of ideas it's very frustrating.
Given an array of options
Array
(
[0] => Array
(
[option_id] => 19
[name] => Colors
)
[1] => Array
(
[option_id] => 16
[name] => Fabric
)
[2] => Array
(
[option_id] => 12
[name] => Size
)
)
And my products array based on color
Array
(
[0] => Array
(
[product_id] => 54
[model] => 01035
[sku] => 050102/bs
)
[1] => Array
(
[product_id] => 57
[model] => 01038
[sku] => 050104/b
)
[2] => Array
(
[product_id] => 58
[model] => 01039
[sku] => 050106/C
)
)
stdClass Object
(
[10010] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1
[value] => option_name_1-option_name_2-option_name_3:::0
[product_option_value] => stdClass Object
(
[12] => stdClass Object
(
[option_value_id] => 849
[product_option_value_id] =>
[model] => repellendus nemo ve-050102/bs
)
[15] => stdClass Object
(
[option_value_id] => 861
[product_option_value_id] =>
[model] => repellendus nemo ve-050104/b
)
)
)
[10011] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1-option_name_2-option_name_3
[value] => option_name_1-option_name_2-option_name_3:::0-849
[product_option_value] => stdClass Object
(
[13] => stdClass Object
(
[option_value_id] => 2407
[product_option_value_id] =>
[model] => repellendus nemo ve-050102/bs-050102/bs
)
)
)
[10012] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1-option_name_2-option_name_3
[value] => option_name_1-option_name_2:::0-849-2407
[product_option_value] => stdClass Object
(
[14] => stdClass Object
(
[option_value_id] => 2884
[product_option_value_id] =>
[model] => repellendus nemo ve-050102/bs-050102/bs-050102/bs
)
)
)
[10013] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1-option_name_2
[value] => option_name_1-option_name_2:::0-861
[product_option_value] => stdClass Object
(
[16] => stdClass Object
(
[option_value_id] => 2407
[product_option_value_id] =>
[model] => repellendus nemo ve-050104/b-050102/bs
)
)
)
[10014] => stdClass Object
(
[product_option_id] =>
[name] => option_name_1-option_name_2-option_name_3
[value] => option_name_1-option_name_2-option_name_3:::0-861-2407
[product_option_value] => stdClass Object
(
[17] => stdClass Object
(
[option_value_id] => 2884
[model] => repellendus nemo ve-050104/b-050102/bs-050102/bs
)
)
)
)
For the moment I've managed building the "first array that holds the base products" but it doesn't have the name concatenated with the option names
$something = function ($cell, $optionName) {
$cOption = $this->getOptionByName($optionName, $cell);
return [
"product_option_id" => "",
'option_value_id' => 0,
'option_val_id' => $cOption['option_value_id'],
"name" => $cOption['name'],
"value" => $cOption['name'],
"option_id" => $cOption['option_id'],
"required" => 0,
"swatch_image" => 0,
"show_first_option_in_list" => "1",
"product_option_value" => []
];
};
$products = [];
$baseProduct = [];
$i = 1000;
for ($c = 4; $c <= 7; $c++) {
if (!empty($cell[$c])) {
$baseProduct = $another($cell[$c], 'Related Colors');
$baseProduct['name'] = $baseProduct['name'] . ':::0';
$baseProduct['value'] = $baseProduct['value'] . ':::0';
$product = $this->getProductBySku($cell[$c]);
$relatedColors[] = $product;
}
}
$baseProduct['product_option_value'] = $relatedColors;
$products[$i++] = $baseProduct;
And this is a visual representation of how on the front should display if the array was build correctly.
End result

Related

How to replace array value with array value [have two array]

I try this array with array_merge() not working.
I have array (array one);
Array (
[0] => mf_3
[1] => mf_2
[2] => mf_1
[3] => mf_7
[4] => mf_6
[5] => mf_4
)
And on array again (second array),
Array (
[0] => stdClass Object
(
[id] => 1
[name] => review_smartphone_display
[label] => Layar
[post_type] => review_smartphone
)
[1] => stdClass Object
(
[id] => 2
[name] => review_smartphone_launch
[label] => Peluncuran
[post_type] => review_smartphone
)
[2] => stdClass Object
(
[id] => 3
[name] => review_smartphone_platform
[label] => Platform
[post_type] => review_smartphone
)
[3] => stdClass Object
(
[id] => 4
[name] => review_smartphone_camera
[label] => Kamera
[post_type] => review_smartphone
)
[4] => stdClass Object
(
[id] => 6
[name] => review_smartphone_design
[label] => Desain
[post_type] => review_smartphone
)
[5] => stdClass Object
(
[id] => 7
[name] => review_smartphone_battery
[label] => Baterai
[post_type] => review_smartphone
)
)
Now, how to replace second array [id] with value from array one.
I want the result like this;
Array (
[0] => stdClass Object
(
[id] => mf_3
[name] => review_smartphone_display
[label] => Layar
[post_type] => review_smartphone
)
[1] => stdClass Object
(
[id] => mf_2
[name] => review_smartphone_launch
[label] => Peluncuran
[post_type] => review_smartphone
)
[2] => stdClass Object
(
[id] => mf_1
[name] => review_smartphone_platform
[label] => Platform
[post_type] => review_smartphone
)
[3] => stdClass Object
(
[id] => mf_7
[name] => review_smartphone_camera
[label] => Kamera
[post_type] => review_smartphone
)
[4] => stdClass Object
(
[id] => mf_6
[name] => review_smartphone_design
[label] => Desain
[post_type] => review_smartphone
)
[5] => stdClass Object
(
[id] => mf_4
[name] => review_smartphone_battery
[label] => Baterai
[post_type] => review_smartphone
)
)
Please help, thanks.
Make use of foreach like below
foreach($first_array as $index => $farray)
{
$second_array[$index]->id = $farray;
}
foreach ($first_array as $key => $value) {
$second_array[$key]->id = $value;
}

Iterating recursively through an array

Update: I have a solution - please see below for details.
I have an array where the keys are levels (in a navigation tree for example) - something like
Array
(
[0] => Array
(
[100] => Array
(
[name] => foo100
[slug] => foo100
[id] => 100
[parent] => 0
[level] => 0
)
[101] => Array
(
[name] => foo101
[slug] => foo101
[id] => 101
[parent] => 0
[level] => 0
)
)
[1] => Array
(
[200] => Array
(
[name] => foo200
[slug] => foo200
[id] => 200
[parent] => 100
[level] => 1
)
[201] => Array
(
[name] => foo201
[slug] => foo201
[id] => 201
[parent] => 101
[level] => 1
)
)
[2] => Array
(
[300] => Array
(
[name] => foo300
[slug] => foo300
[id] => 300
[parent] => 200
[level] => 2
)
[301] => Array
(
[name] => foo301
[slug] => foo301
[id] => 301
[parent] => 201
[level] => 2
)
)
[3] => Array
(
[400] => Array
(
[name] => foo400
[slug] => foo400
[id] => 400
[parent] => 300
[level] => 3
)
)
[4] => Array
(
[500] => Array
(
[name] => foo500
[slug] => foo500
[id] => 500
[parent] => 400
[level] => 4
)
)
)
I need to create an array from this which iterates from the top most level and creates an array with the key being the slug of that level - to produce the following:
Array
(
[foo500] => Array
(
[4] => Array
(
[name] => foo500
)
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo400] => Array
(
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo300] => Array
(
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo301] => Array
(
[2] => Array
(
[name] => foo301
)
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo200] => Array
(
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo201] => Array
(
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo100] => Array
(
[0] => Array
(
[name] => foo100
)
)
[foo101] => Array
(
[0] => Array
(
[name] => foo101
)
)
)
I hope this explains the issue - struggling to get this right! Any help much appreciated!
Update - solution.
For this I removed the first level of keys to leave
Array
(
[107] => Array
(
[id] => 107
[name] => About Us
[indexID] => about
[level] => 0
[parent] => 0
)
[109] => Array
(
[id] => 109
[name] => Home
[indexID] => index
[level] => 0
[parent] => 0
)
}
etc etc
Assuming $data is the above array I went with:
foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
$bc[$v['indexID']][0]['name'] = $v['name'];
$bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
$nextParent = $v['parent'];
$currentIndexID = $v['indexID'];
$currentName = $v['name'];
$bc[$v['indexID']][0]['name'] = $currentName;
$bc[$v['indexID']][0]['indexID'] = $currentIndexID;
for($i=1;$i<=$level;$i++) {
foreach ($data as $a => $b) {
if ($a == $nextParent) {
$nextParent = $b['parent'];
$bc[$v['indexID']][$i]['name'] = $b['name'];
$bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
}
}
}
}
}

How to sort object array by value deep inside array

I have this array:
stdClass Object
(
[tid] => 26001835
[vid] => 5
[name] => AppleTV
[description] => My description
[format] => filtered_html
[weight] => 0
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608990
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => Apple-TV.png
[uri] => public://Apple-TV.png
[filemime] => image/png
[filesize] => 2103
[status] => 1
[timestamp] => 1405346182
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.something.com
[format] =>
[safe_value] => http://www.something.com
)
)
)
[field_learn_more] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://something.com/somepage
[format] =>
[safe_value] => http://something.com/somepage
)
)
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => network
)
)
)
)
stdClass Object
(
[tid] => 26001834
[vid] => 5
[name] => Playstation - USA
[description] => My description
[format] => filtered_html
[weight] => 2
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608991
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => ps4network.png
[uri] => public://ps4network.png
[filemime] => image/png
[filesize] => 4566
[status] => 1
[timestamp] => 1405346218
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.somesite.com
[format] =>
[safe_value] => http://somesite.com
)
)
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => blast_areas
)
)
)
)
stdClass Object
(
[tid] => 26001836
[vid] => 5
[name] => Brighthouse Networks
[description] => My description
[format] => filtered_html
[weight] => 3
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608993
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => brighthouse.png
[uri] => public://brighthouse.png
[filemime] => image/png
[filesize] => 8392
[status] => 1
[timestamp] => 1405358781
)
)
)
[field_buy_now_button_link] => Array
(
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => ppv_provider
)
)
)
)
I want to sort the array by the array by the value in field_device_category. Basically, I want to group the results but first I need to make sure all the objects are sorted by field_device_category.
Thanks in advance!
Use usort for this kind of sorting. It uses Quicksort in the background and takes a user-defined function to compare the array elements:
usort($array, "complicatedArrayComparer");
function complicatedArrayComparer($a,$b)
{
if ($a['field_device_category'] == $b['field_device_category']) {
return 0;
}
return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1;
}

How to extract multiple elements of an array into a new array

My question is how would I take this array:
Array (
[0] => stdClass Object (
[item] => 0
[size] => 2657017
[group] => MAXAT
[description] => 265/70R17 MAXTRAC A/T 115T 00K
[sort4] => 115
[sort5] => T
[sort6] =>
[price] => 118.91
)
[1] => stdClass Object (
[item] => 8127
[size] => 2657017
[group] => FZSUV
[description] => 265/70R17 FUZION SUV OWL 115T 50K
[sort4] => 115
[sort5] => T
[sort6] =>
[price] => 137.81
)
[2] => stdClass Object (
[item] => 0
[size] => 2657017
[group] => MAXAT
[description] => LT265/70R17 MAXTRAC A/T 118S E 00K
[sort4] => 118
[sort5] => S
[sort6] => E
[price] => 153.79
)
[3] => stdClass Object (
[item] => 1237
[size] => 2657017
[group] => ATS
[description] => 265/70R17 GEO AT-S OWL 113S 50K
[sort4] => 113
[sort5] => S
[sort6] =>
[price] => 167.15
)
)
and turn it into this array (without running another query):
Array (
[0] => stdClass Object (
[group] => MAXAT
[price] => 118.91
)
[1] => stdClass Object (
[group] => FZSUV
[price] => 137.81
)
[2] => stdClass Object (
[group] => MAXAT
[price] => 153.79
)
[3] => stdClass Object (
[group] => ATS
[price] => 167.15
)
)
All that I am trying to achieve is to pull the group and price from the first array into a new array.
$newEntries = array();
foreach ($originalEntries as $originalEntry) {
$newEntry = new stdClass();
$newEntry->group = $originalEntry->group;
$newEntry->price = $originalEntry->price;
$newEntries[] = $newEntry;
}
Could you use foreach to loop through the array and insert values into new array?!
$new_array = array();
$i = 0;
foreach($array as $k => $v){
$new_array[$i]['group'] = $v['group'];
$new_array[$i]['price'] = $v['price'];
$i++;
}

CakePHP and Set::combine

I need to get the data for each language in it's field as an array.
Probably the best approach in CakePHP will be Set::combine but can't get it working.
I can do it manually with foreach but I don't think that will be the best way.
Here is the example:
Array
(
[Article] => Array
(
[id] => 131
[title] => TEST
)
[titleTranslation] => Array
(
[0] => Array
(
[id] => 62
[locale] => eng
[model] => Article
[foreign_key] => 131
[field] => title
[content] => TEST
)
[1] => Array
(
[id] => 63
[locale] => fre
[model] => Article
[foreign_key] => 131
[field] => title
[content] => Salva
)
[2] => Array
(
[id] => 64
[locale] => rus
[model] => Article
[foreign_key] => 131
[field] => title
[content] => Пвет
)
)
)
into this array:
Array
(
[Article] => Array
(
[id] => 131
[title] => Array
(
[eng] => TEST
[fre] => Salva
[rus] => Пвет
)
)
.... the rest is not important
)
Solved-----
$translatedData = Set::combine($this->data['titleTranslation'], '{n}.locale', '{n}.content', '{n}.field');
$this->data['Article'] = array_merge($this->data['Article'], $translatedData);

Categories