简体   繁体   English

两个数组之间的多维数组组合

[英]Multidimensional array combinations between two arrays

I'm working on this project written in PHP which is essentially a webshop and I need to generate some XML showing which products are for sale. 我正在从事这个用PHP编写的项目,该项目本质上是一个网上商店,我需要生成一些XML以显示要出售的产品。

Now, each product itself can have what's called attributes like, for example, height, color, shape, etc. And each attribute can have it's own properties like height. 现在,每个产品本身都可以具有所谓的属性,例如高度,颜色,形状等。每个属性都可以具有自己的属性,例如高度。 Height could be 120cm, 150cm, 180cm, etc.. 高度可以是120cm,150cm,180cm等。

There is no limit to how many attributes there can be or how many possible properties can be. 对可以有多少个属性或可以有多少个可能的属性没有限制。

So, how would I find all combinations between theese 2 arrays? 那么,我将如何找到这两个数组之间的所有组合?

Heres an example of a really stripped down product json: Let's just, for all intense of purposes, call this array $products once decoded. 这里是一个真正精简的产品json的示例:就所有强烈的目的而言,一旦解码,就将该数组称为$ products。

{
    {
        "product_id":"1",
        "product_name":"Blommebladet tj\u00f8rn",
        "product_price":"400",
    },
    {
        "product_id":"2",
        "product_name":"Some other product",
        "product_price":"300",
    },
}

Looping through the products themselves is not hard. 遍历产品本身并不难。 But, when looping through the products and getting the properties, it returns a new array which contains all attributes of the product and their properties. 但是,当遍历产品并获取属性时,它将返回一个新数组,其中包含产品的所有属性及其属性。 That's fairly easy too. 这也很容易。

I need the system to generate a new list of products that has all the possible combinations of products without having to think about dealing with an extra array. 我需要系统生成具有所有可能产品组合的新产品列表,而不必考虑处理额外的阵列。 I figured just making a new array of products and cramming the information on to those by copying the exsisting product and adding attributes/properties on to the existing info. 我想只是制作了一系列新产品,并通过复制现有产品并将属性/属性添加到现有信息中,将信息塞入其中。

Attribute/Property stripped down version. 属性/属性精简版本。 This attribute belongs to the first product on my product list. 此属性属于我的产品列表中的第一个产品。

[{
    "attribute_name": "H\u00f8jde",
    "properties": {
        "359": {
            "property_name": "125 cm"
        },
        "356": {
            "property_name": "150 cm"
        },
        "357": {
            "property_name": "180 cm"
        },
        "358": {
            "property_name": "220 cm"
        }
    }
}] 

And here's an example of what I would like to happen. 这是我想发生的事的一个例子。 I'd like a new array, let's just call it $variants. 我想要一个新的数组,让我们称之为$ variants。

This should just copy the base products and add on the extra information to make them new "products". 这应该只是复制基本产品,并添加额外的信息以使其成为新的“产品”。 It's still the same base product so the id can't be changed. 它仍然是相同的基本产品,因此无法更改ID。

The name should change to something like "Prod_name - [{Attr_name Prop_name},]" For example. 例如,名称应更改为“ Prod_name-[{Attr_name Prop_name},]”。 like: 喜欢:

Blommebladet tj\ørn - H\øjde 125cm, Blommebladet tj \\ u00f8rn-H \\ u00f8jde 125cm,
Blommebladet tj\ørn - H\øjde 150cm, Blommebladet tj \\ u00f8rn-H \\ u00f8jde 150cm,
Blommebladet tj\ørn - H\øjde 160cm Blommebladet tj \\ u00f8rn-H \\ u00f8jde 160cm

Hope you get what I mean and help me! 希望你能理解我的意思并帮助我! Thanks in advance. 提前致谢。

You should look for combination algorithm that returns the value of x (attributes in your case) without repetitions. 您应该寻找不会重复的返回x值(在您的情况下为属性)的组合算法。 I can provide you with such, but written in C and you might find it useful and implement the logic in the language that your prefer. 我可以为您提供此类代码,但是用C编写,您可能会发现它很有用,并以您喜欢的语言实现了逻辑。

#include <stdio.h>
#define MAXN 20
// Finds all combinations of n from k
const unsigned n = 5;
const unsigned k = 3;
unsigned mp[MAXN];
void print(unsigned length)
{
    unsigned i;
    for (i = 0; i < length; i++) printf("%u ", mp[i]);
    printf("\n");
}
void comb(unsigned i, unsigned after)
{
    unsigned j;
    if (i > k) return;
    for (j = after + 1; j <= n; j++) {
        mp[i - 1] = j;
        if (i == k) print(i);
        comb(i + 1, j);
    }
}

int main() {
    printf("C(%u,%u): \n", n, k);
    comb(1, 0);
    return 0;
}


    /* Results:
    1 2 3
    1 2 4
    1 2 5
    1 3 4
    1 3 5
    1 4 5
    2 3 4
    2 3 5
    2 4 5
    3 4 5  
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM