简体   繁体   English

合并两个数组

[英]Merge two array in one

Hi have problem to merge array 嗨有合并数组的问题

My first array is product options 我的第一个产品是产品选择

Second array is product variations 第二类是产品变化

I need to duplicate my option array values while values quantity equal to second array's value quantity. 我需要复制我的选项数组值,而值数量等于第二个数组的值数量。

For Eg: 例如:

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

in this array's values is equal 2 = 2 But my $keys array's has only one value 在这个数组的值等于2 = 2但是我的$ keys数组只有一个值

$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"
]

Second array's output is: 第二个数组的输出是:

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

First array may be Color-Size-Material-Type.... Second array may be Red-SA-Cotton... Please Help Me 第一个数组可能是颜色,大小,材料类型...。第二个数组可能是红色-SA-棉花...请帮助我

I need output like this: 我需要这样的输出:

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

Just use array_combine() - 只需使用array_combine() -

array_combine — Creates an array by using one array for keys and another for its values array_combine —通过使用一个数组作为键并使用另一个数组作为其值来创建数组

Example: 例:

$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'
# ]

Edit: 编辑:

$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>';
}

You can use array_map , array_combine 您可以使用array_maparray_combine

$inputs = ["Red-S-Cotton", "Blue-Silk-M"];
$keys   = ['Color-Size-Material', 'Color-Material-Size'];
$res = array_map(function($v1, $v2) {
    return array_combine(explode('-', $v2), explode('-', $v1));
}, $inputs, $keys);
echo '<pre>';
print_r($res);

OR 要么

$inputs = ["Red-S-Cotton", "Blue-Silk-M"];
$keys   = ['Color-Size-Material', 'Color-Material-Size'];
$res = [];
array_walk($inputs, function($v,$k) use ($keys, &$res){
  $res[] = array_combine(explode('-', $keys[$k]), explode('-', $v));
});
echo '<pre>';
print_r($res);

Result 结果

Array
(
[0] => Array
    (
        [Color] => Red
        [Size] => S
        [Material] => Cotton
    )

[1] => Array
    (
        [Color] => Blue
        [Material] => Silk
        [Size] => M
    )

)

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

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