我在該數組中有一個數組我想要合併元素,但條件是如果鍵「HotelCode」與另一個元素的「HotelCode」匹配,則合併,否則將其保留。
MYARRAY如何合併鍵值匹配的數組元素
Array
(
[0] => Array
(
[0] => Array
(
[HotelCode] => ASSOC01
[Price] => Array
(
[RoomPrice] => 200
)
)
[1] => Array
(
[HotelCode] => ASSOC02
[Price] => Array
(
[RoomPrice] => 210
)
)
[2] => Array
(
[HotelCode] => ASSOC03
[Price] => Array
(
[RoomPrice] => 220
)
)
[3] => Array
(
[HotelCode] => ASSOC04
[Price] => Array
(
[RoomPrice] => 230
)
)
)
[1] => Array
(
[0] => Array
(
[HotelCode] => ASSOC02
[Price] => Array
(
[RoomPrice] => 310
)
)
[1] => Array
(
[HotelCode] => ASSOC01
[Price] => Array
(
[RoomPrice] => 300
)
)
[2] => Array
(
[HotelCode] => ASSOC03
[Price] => Array
(
[RoomPrice] => 320
)
)
[3] => Array
(
[HotelCode] => ASSOC04
[Price] => Array
(
[RoomPrice] => 330
)
)
)
[2] => Array
(
[0] => Array
(
[HotelCode] => ASSOC03
[Price] => Array
(
[RoomPrice] => 420
)
)
[1] => Array
(
[HotelCode] => ASSOC01
[Price] => Array
(
[RoomPrice] => 400
)
)
[3] => Array
(
[HotelCode] => ASSOC02
[Price] => Array
(
[RoomPrice] => 410
)
)
)
)
例如:
在MYARRAY有多個陣列,其陣列我想插入陣列,但條件是我要檢查HotelCode如果匹配,則鍵值匹配或不匹配,否則保留它。
像下面陣列:
Array
(
[0] => Array
(
[HotelCode] => ASSOC01
[Room] => Array
(
[0] => Array
(
[RoomPrice] => 200
)
[1] => Array
(
[RoomPrice] => 300
)
[2] => Array
(
[RoomPrice] => 400
)
)
)
[1] => Array
(
[HotelCode] => ASSOC02
[Room] => Array
(
[0] => Array
(
[RoomPrice] => 210
)
[1] => Array
(
[RoomPrice] => 310
)
[2] => Array
(
[RoomPrice] => 410
)
)
)
[2] => Array
(
[HotelCode] => ASSOC03
[Room] => Array
(
[0] => Array
(
[RoomPrice] => 220
)
[1] => Array
(
[RoomPrice] => 320
)
[2] => Array
(
[RoomPrice] => 420
)
)
)
)
在上面數組,你可以看到我做了什麼。
MyArray[0][0][HotelCode], MyArray[1][1][HotelCode] and MyArray[2][1][HotelCode] values are same then i kept in an array
但你可以看到
MyArray[0][3][HotelCode] and MyArray[1][3][HotelCode] are same but that "HotelCode" value is not available in MyArray[2] than i just left it.
Array_merge是你 – CiroRa
我需要用array_merge(),因爲我已經嘗試 – User97798