0

I have this code php to create json file:

foreach ($points as $key => $point) {
$pointsArray[$id]['id']  = $id = $id + 1 ;
$pointsArray[$id]['marker']  = $icon ;
$pointsArray[$id]['name'] = $point->name ;
$pointsArray[$id]['lat']  = $point->lat ;
$pointsArray[$id]['lng']  = $point->lng ;
$pointsArray[$id]['photo'] = $point->images;
$pointsArray[$id]['url']  = $url ;

}

return json_encode($pointsArray);

This is work fine. This return:

{"1":{"id":1,"marker":"greenIcon","name":"nazwa","lat":"19.09411780","lng":"49.81325260","photo":"upload\/images\/UserImage\/8ae2bcb47491c56c80c04b7638378955.jpg","url":"http:\/\/domain.test\/nazwa,u,2"},"2":{"id":2,"marker":"redIcon","name":"LAYLA 38-RIMING W100","lat":"21.33532170","lng":"51.91559550","photo":"upload\/images\/UserImage\/b4041e62f87320ce28d1e5f5434a5f5a.jpg","url":"http:\/\/domain.test\/layla-38-riming-w100,u,3"},"3":{"id":3,"marker":"redIcon","name":"sponsor","lat":"18.40476347","lng":"54.56898020","photo":"upload\/images\/UserImage\/cb36bc68e918606fd3d43ba5e8cd4667.jpg","url":"http:\/\/domain.test\/sponsor,u,4"},"4":{"id":4,"marker":"redIcon","name":"angelsa","lat":"18.39690080","lng":"54.57001480","photo":"upload\/images\/UserImage\/0a7bc1aa9c30d717533f36194ced5fa5.jpg","url":"http:\/\/domain.test\/angelsa,u,5"},"5":{"id":5,"marker":"redIcon","name":"sponsorowana jula","lat":"18.40617210","lng":"54.56171135","photo":"upload\/images\/UserImage\/0d205f080443a779a6e5615e785a9729.jpg","url":"http:\/\/domain.test\/sponsorowana-jula,u,6"},"6":{"id":6,"marker":"redIcon","name":"ananasowy kuba\u0144ski las","lat":"18.40476347","lng":"54.56898020","photo":"upload\/images\/UserImage\/b4311e82d5966da091b9e996a7eeac78.jpg","url":"http:\/\/domain.test\/ananasowy-kubanski-las,u,7"}}

I need this format:

[
  {
    "id": 0,
    "marker": "greenIcon",
    "Name": "Name 01",
    "lat": "54.35070881441067",
    "lng": "18.641191756395074",
    "photo": "https://v.wpimg.pl/MjQzOTQ5YgswGDlnfk5vHnNAbT04F2FIJFh1dn56YlxlTjdgYwdiDn8NPz08UmMaPVV-Z2UMfl9pTHZifBh8UmJIfmdiAXhTfhsjMTpGLAQ1CC55NUAhATgfODcrGycaNlgy",
    "url": "http://www.mydomainm/pages/1"
  },
  {
    "id": 1,
    "marker": "greenIcon",
    "Name": "Name 02",
    "lat": "9.423300",
    "lng": "43.134600",
    "photo": "https://v.wpimg.pl/MjQzOTQ5YgswGDlnfk5vHnNAbT04F2FIJFh1dn56YlxlTjdgYwdiDn8NPz08UmMaPVV-Z2UMfl9pTHZifBh8UmJIfmdiAXhTfhsjMTpGLAQ1CC55NUAhATgfODcrGycaNlgy",
    "url": "http://www.mydomainm/pages/2"
  }
]

How can I change my json format to this destination json?

1
  • What do you mean by "this format"? What's the difference? Commented Jun 14, 2019 at 12:54

4 Answers 4

1

Make an array of elements (not key-value pairs).

$pointsArray = [];
foreach ($points as $key => $point) {
   $element['id']  = $id = $id + 1 ;
   $element['marker']  = $icon ;
   $element['name'] = $point->name ;
   $element['lat']  = $point->lat ;
   $element['lng']  = $point->lng ;
   $element['photo'] = $point->images;
   $element['url']  = $url ;

   $pointsArray[] = $element;
}

return json_encode($pointsArray);
Sign up to request clarification or add additional context in comments.

1 Comment

Incidentally, ++$id could be used in place of $id = $id + 1 here
0

So as You have Said

foreach ($points as $key => $point) {
$pointsArray[$id]['id']  = $id = $id + 1 ;
$pointsArray[$id]['marker']  = $icon ;
$pointsArray[$id]['name'] = $point->name ;
$pointsArray[$id]['lat']  = $point->lat ;
$pointsArray[$id]['lng']  = $point->lng ;
$pointsArray[$id]['photo'] = $point->images;
$pointsArray[$id]['url']  = $url ;

}

$arrayEncoded = json_encode($pointsArray);

$decoded = (json_encode(array_values(json_decode($arrayEncoded,true))));

So the Josn will Look Like You want try

dd($decoded);

Comments

0
   Try this : 
        $i=0;
        foreach ($points as $key => $point) {
        $pointsArray['id'][$i]  = $id = $id + 1 ;
        $pointsArray['marker'][$i]  = $icon ;
        $pointsArray['name'][$i] = $point->name ;
        $pointsArray['lat'][$i]  = $point->lat ;
        $pointsArray['lng'][$i]  = $point->lng ;
        $pointsArray['photo'][$i] = $point->images;
        $pointsArray['url'][$i]  = $url ;
        $i++;
        }

        return json_encode($pointsArray);

Comments

0

You can use Laravel Resource

Example

Creating Resource

php artisan make:resource Points

Controller Application Use the resource class you created

use App\Http\Resources\Points as PointsResource;

Put this on your method

return new PointsResource($pointsArray);

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.