-2

I have an array like this:

 $allauto = [
        'name' => $title,
        'type' => 'simple',
        'description' => $description,
        'attributes' => [
           [
               'id' => 1,
               'visible' => true,
               'options' => $model,
           ],

And I have an array $addimage like this:

$addimage = [
           'images' [
                'src' => xxxxx
                'src' => yyyyy
                 ],
             ]

How do I combine those (with array_push)?so I get a result like tthis:

$allauto = [
            'name' => $title,
            'type' => 'simple',
            'description' => $description,
            'attributes' => [
               [
                   'id' => 1,
                   'visible' => true,
                   'options' => $model,
               ],
            'images' => [
               [
                    'src' => xxxxx
                    'src' => yyyyyy
               ]

I tried different things with array_push but I get keys like 0 and 1 between the 2 arrays... Can anyone help?

Redman
  • 133
  • 1
  • 1
  • 10

3 Answers3

2

First of alĺ, you should check all that missing braces and unexpected commas. But if you are seeking an answer to your question, you could use array_merge to merge these two arrays.

Fixed version:

$allauto = [
    'name' => $title,
    'type' => 'simple',
    'description' => $description,
    'attributes' => [
       [
           'id' => 1,
           'visible' => true,
           'options' => $model
       ]
    ]
 ];
$addimage = [
       'images' => [
            'src' => "yyyyy"
             ]
         ];

var_dump(array_merge($allauto, $addimage));

//Output:
array(5) {
    ["name"]=> string(3) "SDS"
    ["type"]=> string(6) "simple"
    ["description"]=>string(2) "SD"
    ["attributes"]=>array(1) {
                    [0]=> array(3) {
                            ["id"]=> int(1)
                            ["visible"]=> bool(true)
                            ["options"]=> string(4) "SDFF"
                        }
        }
    ["images"]=> array(1) {
                ["src"]=> string(5) "yyyyy"
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
René Beneš
  • 448
  • 1
  • 13
  • 29
0
$allauto['images'] = [
            'src1' => 'xxxxx',
            'src2' => 'yyyyy'
         ];

Try with $allauto['images'] this instead of new variable

HP371
  • 860
  • 11
  • 24
0

If you have associative arrays just use the + operator:

<?php

$alpha = [
    'name' => 'Adam',
    'type' => 'person',
];

$beta = [
    'image' => 'man'
];

$out = $alpha + $beta;

var_export($out);

Output:

array (
    'name' => 'Adam',
    'type' => 'person',
    'image' => 'man',
  )

From the manual:

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

Progrock
  • 7,373
  • 1
  • 19
  • 25