我對如何在作為陣列陣列的一部分的 araay 中推送值有點困惑。
我的情況如下。
我有兩個陣列:
第一個 ( $suggestedRestaurantsGoogleResponse) 是一組 google 地方回應,如下所示:
^ array:2 [▼
0 => array:25 [?]
1 => array:25 [?]
]
第二個 ( $foundRestaurants) 是 DB 查詢的結果,如下所示:
^ 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 ?}
]
我需要實作的是在里面插入,所以我想做的是以下$suggestedRestaurantsGoogleResponse[i]屬性:$foundRestaurants[i]
$suggestedRestaurantsGoogleResponseWithId = array();
foreach($suggestedRestaurantsGoogleResponse as $dataResponse){
foreach($foundRestaurants as $place){
foreach($place as $foundPlace){
array_push($dataResponse, $foundPlace->getAttributes());
array_push($suggestedRestaurantsGoogleResponseWithId, $dataResponse);
}
};
};
但不幸的是,它似乎無法正常作業,就像dd($suggestedRestaurantsGoogleResponseWithId)我得到以下資訊一樣:
array:4 [▼
0 => array:26 [?]
1 => array:27 [?]
2 => array:26 [▼
"address_components" => array:8 [?]
"adr_address" => "<span hljs-string">">Via di S. Cosimato, 14</span>, <span hljs-string">">00153</span> <span hljs-string">">Roma</span> <span hljs-string">">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 hljs-string">">Via di S. Cosimato, 14</span>, <span hljs-string">">00153</span> <span hljs-string">">Roma</span> <span hljs-string">">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"
]
]
]
兩個陣列$foundRestaurants都$suggestedRestaurantsGoogleResponse具有相同的順序,這意味著如果我在$foundRestaurant記錄的第一個位置是關于 ID = 1 的餐廳,$suggestedRestaurantsGoogleResponse首先是 ID = 1 的餐廳的詳細回應。從上面dd(),它似乎復制了每個陣列值,而且,在副本中,他插入了找到的兩家餐廳的屬性。
很抱歉我發布了很長的內容,但我希望你清楚我的想法。
有沒有人知道我做錯了什么?
謝謝
uj5u.com熱心網友回復:
你寫的和你想要的不一樣。試試這個:
$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);
}))
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/465752.html
上一篇:在Laravel的地圖函式中轉儲
