I'm a bit confused on how to push a value inside an araay which is part of an array of arrays.
The situation i have i sthe following.
I have two arrays :
The first ($suggestedRestaurantsGoogleResponse
), is an array of google places responses like the following :
^ array:2 [▼
0 => array:25 [▶]
1 => array:25 [▶]
]
The second ($foundRestaurants
) is the result of a DB query as the following :
^ array:2 [▼
0 => Illuminate\Database\Eloquent\Collection {#1273 ▼
#items: array:1 [▼
0 => App\Models\Restaurant {#1263 ▼
#fillable: array:2 [▶]
#connection: "mysql"
#table: "restaurants"
#primaryKey: "id"
#keyType: "int"
incrementing: true
#with: []
#withCount: []
preventsLazyLoading: false
#perPage: 15
exists: true
wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:5 [▼
"id" => 3
"created_at" => "2022-04-26 18:57:21"
"updated_at" => "2022-04-26 18:57:21"
"property_id" => 1
"google_place_id" => "ChIJfy613ThgLxMR-Kr1iDROgrU"
]
#original: array:5 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
#escapeWhenCastingToString: false
}
1 => Illuminate\Database\Eloquent\Collection {#1254 ▶}
]
What i need to achieve is to insert inside the $suggestedRestaurantsGoogleResponse[i]
, the attributes of the $foundRestaurants[i]
so what i have thought to do is the following :
$suggestedRestaurantsGoogleResponseWithId = array();
foreach($suggestedRestaurantsGoogleResponse as $dataResponse){
foreach($foundRestaurants as $place){
foreach($place as $foundPlace){
array_push($dataResponse, $foundPlace->getAttributes());
array_push($suggestedRestaurantsGoogleResponseWithId, $dataResponse);
}
};
};
But unfortunately it doesn't seem to work properly as if i dd($suggestedRestaurantsGoogleResponseWithId)
i get the following :
array:4 [▼
0 => array:26 [▶]
1 => array:27 [▶]
2 => array:26 [▼
"address_components" => array:8 [▶]
"adr_address" => "<span >Via di S. Cosimato, 14</span>, <span >00153</span> <span >Roma</span> <span >RM</s ▶"
"business_status" => "OPERATIONAL"
"formatted_address" => "Via di S. Cosimato, 14, 00153 Roma RM, Italy"
"formatted_phone_number" => "06 580 0353"
"geometry" => array:2 [▶]
"icon" => "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/restaurant-71.png"
"icon_background_color" => "#FF9E67"
"icon_mask_base_uri" => "https://maps.gstatic.com/mapfiles/place_api/icons/v2/restaurant_pinlet"
"international_phone_number" => " 39 06 580 0353"
"name" => "Da Vittorio a Trastevere"
"opening_hours" => array:3 [▶]
"photos" => array:10 [▶]
"place_id" => "ChIJ0YUORThgLxMRsjt84lb-KUk"
"plus_code" => array:2 [▶]
"price_level" => 2
"rating" => 4.3
"reference" => "ChIJ0YUORThgLxMRsjt84lb-KUk"
"reviews" => array:5 [▶]
"types" => array:4 [▶]
"url" => "https://maps.google.com/?cid=5272024487934311346"
"user_ratings_total" => 1204
"utc_offset" => 120
"vicinity" => "Via di San Cosimato, 14, Roma"
"website" => "http://www.davittorioatrastevere.it/"
0 => array:5 [▼
"id" => 3
"created_at" => "2022-04-26 18:57:21"
"updated_at" => "2022-04-26 18:57:21"
"property_id" => 1
"google_place_id" => "ChIJfy613ThgLxMR-Kr1iDROgrU"
]
]
3 => array:27 [▼
"address_components" => array:8 [▶]
"adr_address" => "<span >Via di S. Cosimato, 14</span>, <span >00153</span> <span >Roma</span> <span >RM</s ▶"
"business_status" => "OPERATIONAL"
"formatted_address" => "Via di S. Cosimato, 14, 00153 Roma RM, Italy"
"formatted_phone_number" => "06 580 0353"
"geometry" => array:2 [▶]
"icon" => "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/restaurant-71.png"
"icon_background_color" => "#FF9E67"
"icon_mask_base_uri" => "https://maps.gstatic.com/mapfiles/place_api/icons/v2/restaurant_pinlet"
"international_phone_number" => " 39 06 580 0353"
"name" => "Da Vittorio a Trastevere"
"opening_hours" => array:3 [▶]
"photos" => array:10 [▶]
"place_id" => "ChIJ0YUORThgLxMRsjt84lb-KUk"
"plus_code" => array:2 [▶]
"price_level" => 2
"rating" => 4.3
"reference" => "ChIJ0YUORThgLxMRsjt84lb-KUk"
"reviews" => array:5 [▶]
"types" => array:4 [▶]
"url" => "https://maps.google.com/?cid=5272024487934311346"
"user_ratings_total" => 1204
"utc_offset" => 120
"vicinity" => "Via di San Cosimato, 14, Roma"
"website" => "http://www.davittorioatrastevere.it/"
HAVE ALOOK AT THE BELOW :
0 => array:5 [▼
"id" => 3
"created_at" => "2022-04-26 18:57:21"
"updated_at" => "2022-04-26 18:57:21"
"property_id" => 1
"google_place_id" => "ChIJfy613ThgLxMR-Kr1iDROgrU"
]
1 => array:5 [▼
"id" => 4
"created_at" => "2022-04-26 20:39:11"
"updated_at" => "2022-04-26 20:39:11"
"property_id" => 1
"google_place_id" => "ChIJ0YUORThgLxMRsjt84lb-KUk"
]
]
]
Both arrays $foundRestaurants
and $suggestedRestaurantsGoogleResponse
have the same order, this means that if I have on the first place of $foundRestaurant
the record regarding the restaurant with ID = 1, in the $suggestedRestaurantsGoogleResponse
at the first place there's the detail response for the restaurant with ID = 1.
From the above dd(), it seems to duplicate each array value and also, in the the duplicate he's inserting the attributes for both restaurants found.
Sorry for the long stuff i posted but i hope that you have clear my idea.
Is there anybody who has an idea of what i'm doing wrong?
Thank you
CodePudding user response:
What you've wrote and what you want is different. Try this one:
$suggestedRestaurantsGoogleResponseWithId = array();
for($i = 0; $i < count($suggestedRestaurantsGoogleResponse); $i ) {
$suggestedRestaurantsGoogleResponseWithId.push($foundRestaurant[i]->map(function($item) {
return array_map(function($foundPlace) {
return $foundPlace->getAttributes();
},
$item);
}))
}