【问题标题】:Proper way of using Laravel Collect for creating combination使用 Laravel Collect 创建组合的正确方法
【发布时间】:2020-12-27 10:26:12
【问题描述】:

我正在尝试从下面的列表中创建组合,但我不确定如何解析结果,因为结果每次都会动态变化。

这是我通过表单收到的result

{"1":["Medium"],"2":["White","Blue"]}

我如何解析上述结果以获得可能的组合(例如下面)。

product_combination 
-------------------
Medium-White
Medium-Blue
....
....
Medium
Medium-White-Large

我正在使用 Laravel,我稍后会将数据存储在数据库中以供参考

如果您能解释一下如何处理此类数据,我将不胜感激。

编辑: 我想出了一个替代方案

$input = json_decode('{"1":["Medium", "Large"],"2":["White", "Blue"]}', true);
$result = array();
        foreach ($input as $array) {
            $result = array_merge($result, $array);
        }
        print_r(collect($result)->crossJoin(collect($result)));

以上代码结果如下

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [0] => Medium
                    [1] => Medium
                )

            [1] => Array
                (
                    [0] => Medium
                    [1] => White
                )

            [2] => Array
                (
                    [0] => Medium
                    [1] => Blue
                )

            [3] => Array
                (
                    [0] => White
                    [1] => Medium
                )

            [4] => Array
                (
                    [0] => White
                    [1] => White
                )

            [5] => Array
                (
                    [0] => White
                    [1] => Blue
                )

            [6] => Array
                (
                    [0] => Blue
                    [1] => Medium
                )

            [7] => Array
                (
                    [0] => Blue
                    [1] => White
                )

            [8] => Array
                (
                    [0] => Blue
                    [1] => Blue
                )    
        )    
)

在这种情况下,数组是自动填充的,但结果比 PaulH 先生的方法长

【问题讨论】:

标签: php arrays json laravel laravel-5


【解决方案1】:
<?php
// convert json input string to array
$input = json_decode('{"1":["Medium", "Large"],"2":["White", "Blue"]}', true);

// convert both parts of array to collections, 
// crossJoin all the combinations and
// join the combinations in strings  
collect($input[1]) 
    ->crossJoin(collect($input[2]))
    ->map(function($combination) {
        return collect($combination)->join('-');
        
        // alternatively:
        // return implode('-', $combination);
    });

// ['Medium-White', 'Medium-Blue', 'Large-White', 'Large-Blue']

替代方案,没有 Laravel 集合:

<?php
$input = json_decode('{"1":["Medium", "Large"],"2":["White", "Blue"]}', true);

foreach($input[1] as $in1) {
    foreach($input[2] as $in2) {
        $out[] = $in1 . '-' . $in2; 
    }
} 

return $out;
// ['Medium-White', 'Medium-Blue', 'Large-White', 'Large-Blue']

根据 cmets 的要求扩展到“动态”

<?php
function combine($inputs) {
    $inputs = json_decode($inputs, true);
    $results = array_shift($inputs); 
    while ($inputs) {
        $newresults = [];  
        $newinputs = array_shift($inputs);
        foreach ($results as $result) {
            foreach ($newinputs as $new) {
                $newresults[] = $result.'-'.$new;
            }
        }
        $results = $newresults;
    }
    print_r($results);
}

测试:

combine('{"1": ["Medium", "Large"]}');
combine('{"1": ["Medium"], "3": ["Cotton", "Silk"]}');
combine('{"1": ["Medium", "Large"], "2": ["White", "Blue"], "3": ["Cotton", "Silk"]}');

测试结果:

Array
(
    [0] => Medium
    [1] => Large
)
Array
(
    [0] => Medium-Cotton
    [1] => Medium-Silk
)
Array
(
    [0] => Medium-White-Cotton
    [1] => Medium-White-Silk
    [2] => Medium-Blue-Cotton
    [3] => Medium-Blue-Silk
    [4] => Large-White-Cotton
    [5] => Large-White-Silk
    [6] => Large-Blue-Cotton
    [7] => Large-Blue-Silk
)

【讨论】:

  • 感谢 Paul,但我收到此错误 -- 调用数组上的成员函数 join()",异常:
  • 以上列表也是动态的。这可以增长。我该如何处理......例如:{“1”:[“Medium”],“2”:[“White”,“Blue”],“3”:[“Cotton”,“Silk”], .....,...}
  • print_r(collect($input[1]));结果:Illuminate\Support\Collection Object ([items:protected] => Array ([0] => Medium))
  • 更新了上面的 print_r(collect($input[1])); .. 仍然收到错误 --- 调用数组上的成员函数 join()”,异常:
  • 替换 return $combination->join('-'); with implode('-', $combination);
猜你喜欢
  • 2020-12-27
  • 1970-01-01
  • 2013-05-02
  • 2015-12-15
  • 2019-11-02
  • 2015-03-17
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
相关资源
最近更新 更多