Home > OS >  How to print an array inside other array but without "0" index?
How to print an array inside other array but without "0" index?

Time:07-17

stack Community, I have an array called $gallery[] with his keys and values and i have to insert it to another array called $params[]

$gallery = [
    "image_1": "https://staticw.s3.amazonaws.com/object/12331231.jpg",
    "image_2": "https://staticw.s3.amazonaws.com/object/12312312.jpg",
];

$params = [
            "codpro" => $props[0]->codpro,
            "city_code" => $props[0]->city_code,
            "address" => $props[0]->address,
            "latitude" => $props[0]->latitude,
            "longitude" => $props[0]->longitude,
            $gallery
        ];

return $params;

After i return $params have this:

{
  "0": {
    "image_1": "https://staticw.s3.amazonaws.com/object/12331231.jpg",
    "image_2": "https://staticw.s3.amazonaws.com/object/12312312.jpg",
  },
  "codpro": 5354055,
  "city_code": null,
  "address": "Calle 47 A # 45 A 31",
  "latitude": "3.5426742",
  "longitude": "-76.3173984",
  
}

But i need something like this:

{
  "image_1": "https://staticw.s3.amazonaws.com/object/12331231.jpg",
  "image_2": "https://staticw.s3.amazonaws.com/object/12312312.jpg",
  "codpro": 5354055,
  "city_code": null,
  "address": "Calle 47 A # 45 A 31",
  "latitude": "3.5426742",
  "longitude": "-76.3173984",
  
}

CodePudding user response:

You can use array_merge() php function to get the results. Save the following code in a file, call it array_merge.php and try it in a browser. It should give you your desired results.

<?php

$gallery = [
    "image_1"=> "https://staticw.s3.amazonaws.com/object/12331231.jpg",
    "image_2"=> "https://staticw.s3.amazonaws.com/object/12312312.jpg",
];

$params = [
            "codpro" => "props[0]->codpro",
            "city_code" => "props[0]->city_code",
            "address" => "props[0]->address",
            "latitude" => "props[0]->latitude",
            "longitude" => "props[0]->longitude",
        ];


//return $params;
print_r(array_merge($gallery, $params));

?>

Here are the results when viewed in a browser

Array
(
    [image_1] => https://staticw.s3.amazonaws.com/object/12331231.jpg
    [image_2] => https://staticw.s3.amazonaws.com/object/12312312.jpg
    [codpro] => props[0]->codpro
    [city_code] => props[0]->city_code
    [address] => props[0]->address
    [latitude] => props[0]->latitude
    [longitude] => props[0]->longitude
)

CodePudding user response:

$params['gallery'] = $gallery;

Your $gallery variable also has error in it. It should be like this:

$gallery = [
    "image_1" => "https://staticw.s3.amazonaws.com/object/12331231.jpg",
    "image_2" => "https://staticw.s3.amazonaws.com/object/12312312.jpg"
];
  • Related