我有一個多維數組,我只想保留最重複的條目。我得到的最接近的是:返回包含重複數組的數組php
$wd = array_unique($arr);
$d = array_diff($arr, $wd);
print_r($d);
但這隻適用於單維數組並輸出所有重複。我將如何去做這件事?期望的輸出的
例子:
如果陣列是:
array(
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
)
它應該返回array([1] => ([u] => test1u [d] => test1d))
並且如果陣列是:
array(
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
)
它應該返回array([1] => ([u] => test1u [d] => test1d)[2] => ([u] => test2u [d] => test2d))
但如果數組是:
array(
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[1] => (
[u] => test1u
[d] => test1d
)
)
它應該只返回array([1] => ([u] => test1u [d] => test1d))
編輯:
有數組中的重複條目,因爲數組從$arr = json_decode($arr);
來了,原來的JSON有重複條目。 如果有更好的方法來做到這一點而不解碼json,讓我知道。
這被用作搜索程序的一部分。 JSON是源數組中滿足其中一個搜索項條件的所有條目的數組。保持最重複的條目確保這些條目包含大部分(如果不是全部)搜索條件。
這裏是被解碼的JSON文件:
[{"1":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"2":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"5":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"3":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"4":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"5":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"6":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]}]
在這種情況下,使得這JSON是爲「玫瑰水仙」
如果可能,做一個'var_export()'並更新你的問題會有幫助。 –
怎麼可能是一個具有重複鍵的數組? – hassan
當數組中存在多個相同鍵的實例時,它們將被覆蓋。索引必須是唯一的。 – Qirel