简体   繁体   English

PHP数组操作自定义

[英]Php array manipulation custom

I would like to get Below object in to this format how can i get this i want some one to guide me. 我想以这种格式获取“下面的对象”,我该如何得到这个?我想要一些人来指导我。

This method is for generating number of variants from the product based on its options. 此方法用于根据产品的选项来生成多种变体。

I have tried in static variable in the array loop but it ended up with missing 2-4 variants 我曾在数组循环中尝试过静态变量,但最终却缺少2-4个变体

Example OutPut which i need 我需要的示例输出

Yellow, M, 1Gb
Yellow, M, 2GB
Yellow, S, 1GB
Yellow, S, 2GB
Yellow, L, 1GB
Yellow, L, 2GB


Green, M, 1Gb
Green, M, 2GB
Green, S, 1GB
Green, S, 2GB
Green, L, 1GB
Green, L, 2GB

Object Data 对象数据

[
    [
        {
            "id": 1,
            "variantID": 1,
            "option_name": "Yellow",
            "imageID": 1
        },
        {
            "id": 2,
            "variantID": 1,
            "option_name": "Green",
            "imageID": 2
        }
    ],
    [
        {
            "id": 3,
            "variantID": 2,
            "option_name": "M"
        },
        {
            "id": 4,
            "variantID": 2,
            "option_name": "S"
        },
        {
            "id": 5,
            "variantID": 2,
            "option_name": "L"
        }
    ],
    [
        {
            "id": 5,
            "variantID": 3,
            "option_name": "1GB"
        },
        {
            "id": 6,
            "variantID": 3,
            "option_name": "2GB"
        }
    ]
]

Sorry: not good in english 对不起:英语不好

Plugin: https://github.com/bpolaszek/cartesian-product 插件: https//github.com/bpolaszek/cartesian-product

use function BenTools\CartesianProduct\cartesian_product;

$data = [
    'color' => [
        'yellow',
        'green'
    ],
    'size' => [
        'm',
        's',
        'l',
    ],
    'ram' => [
    '1 gb',
    '2 gb',
]
];

foreach (cartesian_product($data) as $combination) {
    printf('color: %s - size: %s - ram: %s' . PHP_EOL, $combination['color'], $combination['size'],$combination['ram']);
}

OutPUT: 输出:

color: yellow - size: m - ram: 1 gb
color: yellow - size: m - ram: 2 gb
color: yellow - size: s - ram: 1 gb
color: yellow - size: s - ram: 2 gb
color: yellow - size: l - ram: 1 gb
color: yellow - size: l - ram: 2 gb
color: green - size: m - ram: 1 gb
color: green - size: m - ram: 2 gb
color: green - size: s - ram: 1 gb
color: green - size: s - ram: 2 gb
color: green - size: l - ram: 1 gb
color: green - size: l - ram: 2 gb

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

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