首页 > 解决方案 > 处理php数组

问题描述

我的产品具有多个选项值,例如颜色和尺寸。

输入:

$colors = ['red','black','blue'];
$sizes = ['large', 'medium', 'small'];

此输入的预期输出应该是 9 个变体,其中一个应该是:

['red', 'small'];

我是 PHP 新手,那么最好的方法是什么?

标签: php

解决方案


我建议你做类似的事情:

$array1 = ['red', 'blue'];
$array2 = ['large', 'small'];

foreach ($array1 as $value1) {
    foreach ($array2 as $value2) {
        $myNewArray = [$value1, $value2];
        // here you can print array
        // print_r($myNewArray);
    }
}

你会得到这个输出:

['red', 'large']
['red', 'small'] // here it's what you are looking for
['blue', 'large']
['blue', 'small']

推荐阅读