合并两个数组并创建新数组 [英] Merge two array and create new

查看:91
本文介绍了合并两个数组并创建新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建数组时遇到问题.我有表格. 表单的输入是标签输入.首先输入的是选项名称.当您输入任何选项名称时,新标签输入将添加到表单中. 我将数据发布到控制器中,并为产品创建自动变体.例如. Red-S Red-M Blue-S Blue-M ...我正在使用的代码给出了以下变化:

I have problem to create array. I have form. Form's inputs are tag inputs. First input is option name. When you put any option name the new tag input added in the form. I post data in controller and create auto variations for product. Eg. Red-S Red-M Blue-S Blue-M ... The Code which i'm using gives variation look like this:

array:4 [
 0 => "Red-S"
 1 => "Blue-S"
 2 => "Red-M"
 3 => "Blue-M"
]

我还有另一个选项名称数组

And i have another array for option names

array:2 [
 0 => "Color"
 1 => "Size"
 ]

我想创建一个像这样的数组:

I want to create one array look like this:

array:2 [
 "Color" => "Red"
 "Size" => "S" 
 ]

这是我的控制器

function make_combinations($props)
    {
        if ( empty($props) ) return [];
        $keys = array_keys($props);
        $key = $keys[0];
        $values = $props[$key];
        unset($props[$key]);
        $rest =  make_combinations($props);
        if ( empty($values) ) return $rest;
        if ( empty($rest) ) return $values;
        $combinations = [];

        foreach($rest as $comb)
        {
            foreach($values as $value)
            {
                $combinations[] = $value . '-' . $comb;

            }
        }
        return $combinations;
    }

    $inputs = explode(',', $request['option']);
    $props = [];

    foreach($inputs as $input)
    {
        $input_key = strtolower($input);
        if ( !$request->has($input_key) ) continue;
        $input_values = explode(',', $request->input($input_key));
        $props[$input_key] = $input_values;
    }
    $combinations = make_combinations($props);



    $result[] = compact('combinations', 'inputs');






    return view('system.modules.variants', compact('result'));
}

推荐答案

您可以跳过一个循环.

您可以编写类似这样的代码

You can write code something like this

$arr = [
 0 => "Red-S-Silk-Y",
 1 => "Blue-S",
 2 => "Red-M-Cotton",
 3 => "Blue-M",
];

$arrFinal = [];
$dataArray = [];
foreach($arr AS $value){
    $expArr = explode("-",$value);
    if(isset($expArr[0])){
      $dataArray['color'] = $expArr[0];
    }
    if(isset($expArr[1])){
      $dataArray['size'] = $expArr[1];
    }
    if(isset($expArr[2])){
      $dataArray['Material'] = $expArr[2];
    }
    if(isset($expArr[3])){
      $dataArray['Style'] = $expArr[3];
    }
    $arrFinal[] = $dataArray;
    Unset($dataArray);
}

echo "<pre>";
print_r($arrFinal);

您的输出将是这样.

Array
(
    [0] => Array
        (
            [color] => Red
            [size] => S
            [Material] => Silk
            [Style] => Y
        )

    [1] => Array
        (
            [color] => Blue
            [size] => S
        )

    [2] => Array
        (
            [color] => Red
            [size] => M
            [Material] => Cotton
        )

    [3] => Array
        (
            [color] => Blue
            [size] => M
        )
)

这篇关于合并两个数组并创建新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆