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 關注
- 118 瀏覽
添加回答
舉報