合并两个数组 [英] Merge two array in one

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

问题描述

您好,无法合并数组

我的第一个数组是产品选项

My first array is product options

第二个数组是产品变体

我需要复制我的选项数组的值,同时值的数量等于第二个数组的值的数量.

I need to duplicate my option array values while values quantity equal to second array's value quantity.

例如:

$inputs = ["Red-S-Cotton", "Blue-Silk-M"];
$keys = ['Color-Size-Material', 'Color-Material-Size'];

在此数组的值等于2 = 2 但是我的$ keys数组只有一个值

in this array's values is equal 2 = 2 But my $keys array's has only one value

$key = ['Color-Size-Material'];

当我尝试将它们合并时,由于不相等,我会收到错误消息.

when i tried to merge them i get error because there are not equal.

我的数组值和值名称是可变的,并且是随机的.我的代码看起来像这样:

My array values and value names is changeable and get's randomly. i have code look like this:

     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);
    $as = array(implode('-', $inputs));


    $inpu = $combinations;
    $keys =$as;
    $say = count($inpu);
    $say2 = count($keys);
    if($say2 < $say) {

        $output = array_map(function ($key) use ($inpu, $keys) {
            $expKeys = explode('-', $keys[$key]);
            $expInputs = explode('-', $inpu[$key]);
            return array_combine($expKeys, $expInputs);
        }, array_keys($inpu));
        dd($output);
    }else{
        $output = array_map(function ($key) use ($inpu, $keys) {
            $expKeys = explode('-', $keys[$key]);
            $expInputs = explode('-', $inpu[$key]);
            return array_combine($expKeys, $expInputs);
        }, array_keys($inpu));
        dd($output);
    }


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






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

我的第一个数组的输出是:

my first array's output is:

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

第二个数组的输出是:

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

第一个数组可以是Color-Size-Material-Type.... 第二个阵列可能是Red-S-A-棉... 请帮助我

First array may be Color-Size-Material-Type.... Second array may be Red-S-A-Cotton... Please Help Me

我需要这样的输出:

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

推荐答案

只需使用array_combine()-

array_combine —通过使用一个数组作为键并使用另一个数组作为其值来创建数组

array_combine — Creates an array by using one array for keys and another for its values

示例:

$toBeKeys = ['color', 'size'];
$toBeValues = ['green', 'M'];

$array = array_combine($toBeKeys, $toBeValues);

echo '<pre>'. print_r($array, 1) .'</pre>';

# will output
# Array => [
#     'color' => 'green',
#     'size' => 'M'
# ]

$input = ['red-s', 'blue-m'];
$keys = ['color', 'size'];

foreach ($input as $el)
{
    $attr = explode('-', $el);
    $combined = array_combine($keys, $attr);

    echo '<pre>'. print_r($array, 1) .'</pre>';
}

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

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