1 回答
TA贡献1858条经验 获得超8个赞
dr-spec您可以获取所有子数组中的所有项目,将它们合并到一个数组中,然后获取唯一值:
$result = array_unique(array_merge(...array_column($attributes['doctor'], 'dr-spec')));
仅出于学习目的,使用当前代码,您可以检查它是否在数组中以消除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;
或者合并它们以消除第二个循环:
$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 关注
- 76 浏览
添加回答
举报