2 回答
TA贡献1804条经验 获得超3个赞
这就是我要做的:
// I take first element of array as a source for indexes
foreach ($myArray[0] as $index => $item) {
// next I extract all elements from all subarrays under current `$index`
$values = array_column($myArray, $index);
// then I filter values to remove nulls.
// This also removes 0, empty arrays, false,
// so maybe you should change filter process
$values_filtered = array_filter($values);
// if number of filtered items is same as in original array - no nulls found
if (count($values_filtered) === count($values)) {
echo $index;
// optionally
// break;
}
}
TA贡献1906条经验 获得超3个赞
尽管有一个公认的答案,但我想我会分享一种使用 Laravel 集合来做到这一点的方法。
$uniqueKeysWithValues = collect($myArray)->map(function($item){
return array_keys( collect($item)->filter()->toArray() ); //filter will remove all null
})->flatten()->unique();
这种方法将为您提供所有包含值的键,即使两个键中都有值。
- 2 回答
- 0 关注
- 87 浏览
添加回答
举报