我有一個這樣的數組:PHP查找相同的值數組,並創建新的數組
array
0 =>
array (size=5)
'id' => string '1' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string '[email protected]' (length=16)
1 =>
array (size=5)
'id' => string '3' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string '[email protected]' (length=26)
2 =>
array (size=5)
'id' => string '5' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string '[email protected]' (length=23)
3 =>
array (size=5)
'id' => string '410' (length=3)
'hostings_id' => string '5' (length=1)
'email' => string '[email protected]' (length=13)
4 =>
array (size=5)
'id' => string '148' (length=3)
'hostings_id' => string '5' (length=1)
'email' => string '[email protected]' (length=23)
5 =>
array (size=5)
'id' => string '165' (length=3)
'hostings_id' => string '8' (length=1)
'email' => string '[email protected]' (length=15)
6 =>
...
我的目標是把這個數組轉換成一個新的數組:
array
0 =>
array (size=5)
'id' => string '1' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string '[email protected]' (length=16)
array (size=5)
'id' => string '3' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string '[email protected]' (length=26)
array (size=5)
'id' => string '5' (length=1)
'hostings_id' => string '4' (length=1)
'email' => string '[email protected]' (length=23)
1 =>
array (size=5)
'id' => string '410' (length=3)
'hostings_id' => string '5' (length=1)
'email' => string '[email protected]' (length=13)
array (size=5)
'id' => string '148' (length=3)
'hostings_id' => string '5' (length=1)
'email' => string '[email protected]' (length=23)
2 =>
array (size=5)
'id' => string '165' (length=3)
'hostings_id' => string '8' (length=1)
'email' => string '[email protected]' (length=15)
3 =>
最後我希望像這樣的JSON:
{
"emails": {
"0": {
"id": "1",
"hostings_id": "4",
"email": "[email protected]",
},
"1": {
"id": "3",
"hostings_id": "4",
"email": "[email protected]",
},
"2": {
"id": "5",
"hostings_id": "4",
"email": "[email protected]",
},
}
}
{
"emails": {
"0": {
"id": "410",
"hostings_id": "5",
"email": "[email protected]",
},
"0": {
"id": "148",
"hostings_id": "5",
"email": "[email protected]",
},
}
}
...
我想基於「hostings_id」 一切什麼有SA創建JSON我'hostings_id'值應該在一個單獨的JSON中。
我已經嘗試了很多foreach和array_count_values,但沒有得到我想要的結果。 非常感謝您的幫助。
問候
這就是你需要的array_merge($ array1,$ array2); –