Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "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",
  
}

>Solution :

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
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading