简体   繁体   中英

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

$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

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 — Creates an array by using one array for keys and another for its values

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

$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
    )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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