1 回答

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
dr-spec您可以獲取所有子數(shù)組中的所有項(xiàng)目,將它們合并到一個(gè)數(shù)組中,然后獲取唯一值:
$result = array_unique(array_merge(...array_column($attributes['doctor'], 'dr-spec')));
僅出于學(xué)習(xí)目的,使用當(dāng)前代碼,您可以檢查它是否在數(shù)組中以消除array_unique:
foreach( $attributes['doctor'] as $doctor ): // Loop through the top array
foreach( $doctor['dr-spec'] as $spec ): // Loop through the dr-spec array
if(!in_array($spec, $result)) {
$result[] = $spec; // assign string into a new array
}
endforeach;
endforeach;
或者合并它們以消除第二個(gè)循環(huán):
$result = [];
foreach( $attributes['doctor'] as $doctor ): // Loop through the top array
$result = array_merge($result, $doctor['dr-spec']);
endforeach;
$result = array_unique($result);
- 1 回答
- 0 關(guān)注
- 115 瀏覽
添加回答
舉報(bào)