jeudi 26 octobre 2017

php loop throught array and add to another array

What I want to achieve is:

opening_times:

0: 1: 2: etc

What I actually have is:

{
  "message": {
    "entity": {
      "google_places": [
        {
          "opening_times": [
            "Monday: 11:30 AM – 8:30 PM"
          ]
        },
        {
          "opening_times": [
            "Tuesday: 11:30 AM – 8:30 PM"
          ]
        },
        {
          "time": "3 weeks ago",
          "author_name": "Adam Walsh",
          "rating": 4
        }
      ]
    }
  }
}

And my code is:

if (!empty($dirty_googlePlaces_array['result']['opening_hours']['weekday_text'])) {
    foreach ($dirty_googlePlaces_array['result']['opening_hours']['weekday_text'] as $opening) {
        $googlePlaces_array[] = ['opening_times' => [ $opening]];
    }
}
if (!empty($dirty_googlePlaces_array['result']['reviews'])) {
    foreach ($dirty_googlePlaces_array['result']['reviews'] as $review) {
        $author_name = $review['author_name'];
        $time = $review['relative_time_description'];
        $rating = $review['rating'];
    }
}
$googlePlaces_array[] = [
        'time' => $time,
        'author_name' => $author_name,
        'rating' => $rating,
    ];
return response()->json(['message' => ['entity' => [
                                                            'google_places' => $googlePlaces_array
]]], 200);

How can I re-structure that format to have an array:

googlePlaces: opening_times: array here time:time author_name:string rating: number



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hbz27S
via IFTTT

Aucun commentaire:

Enregistrer un commentaire