0

I have 2 given arrys:

Array one (which are the "Headlines"):

Array
(
    [0] => Kompatible Produkte
    [1] => Drucktechnologie
    [2] => Druckfarben
    [3] => BCP-Tintentropfenfarbe
    [4] => Temperaturbereich bei Lagerung
    [5] => Paketgewicht
    [6] => Verpackungsabmessungen (BxTxH)
)

And Array two (which are the "values")

Array
    (
    [0] => HP Designjet 5500, 5500ps, 5000, 5000ps
    [1] => 
    [2] => Black
    [3] => 
    [4] => -40 - 60
    [5] => 230
    [6] => 114 x 36 x 264
)

Now I would like to combine these 2 Arrays in a 3 Array... The array 3 looks like:

$data[] = array( 
    'sku' => '291',    
    '_type' => 'simple', 
    '_attribute_set' => 'Default', 
    '_product_websites' => 'base', 
    'name' => 'C4950A', 
    'manufacturer' => 'HP Inc.',    
    'meta_autogenerate' => 'yes', 
    'short_description' => 'HP 81', 
    'qty' => 2, 
); 

And I would like to have this:

$data[] = array( 
    'sku' => '291',    
    '_type' => 'simple', 
    '_attribute_set' => 'Default', 
    '_product_websites' => 'base', 
    'name' => 'C4950A', 
    'manufacturer' => 'HP Inc.',    
    'meta_autogenerate' => 'yes', 
    'short_description' => 'HP 81', 
    'qty' => 2, 

    'Kompatible Produkte' => 'HP Designjet 5500, 5500ps, 5000, 5000ps',
    'Drucktechnologie' => '',
    'Druckfarben' => 'Black',
    'BCP-Tintentropfenfarbe' => '',
    'Temperaturbereich bei Lagerung' => '-40 - 60',
    'Paketgewicht' => '230',
    'Verpackungsabmessungen (BxTxH)' => '114 x 36 x 264'
); 

The amount of values in array one and two is always the same. I thought it would be possible with "for each", but I can't find a working solution...

Thank you for any help!

3 Answers 3

2

You can do it easily using array_combine and array_merge function:

$data = array_merge($data,array_combine($arr1,$arr2));
print_r($data);
Sign up to request clarification or add additional context in comments.

Comments

1

You can do a foreach loop quite easily this way.

$a = ['a','b','c'];

$b = ['a1','b1','c1'];

$c = [
  'e' => 'e1',
  'd' => 'd1'
];

foreach($a as $key => $value){

    $c[$value] = $b[$key];

}

print_r($c);

Comments

0

Please check below code which i have run in my system.

$arr1 = array("Kompatible Produkte", "Drucktechnologie", "Druckfarben", "BCP-Tintentropfenfarbe", "Temperaturbereich bei Lagerung", "Paketgewicht", "Verpackungsabmessungen (BxTxH)");
$arr2 = array("HP Designjet 5500, 5500ps, 5000, 5000ps", "", "Black", "", "-40 - 60", "230", "114 x 36 x 264");
$arr3 = array(
    'sku' => '291',
    '_type' => 'simple',
    '_attribute_set' => 'Default',
    '_product_websites' => 'base',
    'name' => 'C4950A',
    'manufacturer' => 'HP Inc.',
    'meta_autogenerate' => 'yes',
    'short_description' => 'HP 81',
    'qty' => 2,
);

$tempArr = array_combine($arr1, $arr2);
$result = array_merge($arr3, $tempArr);

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

Simplly use array_combine and array_merge.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.