Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-获取在多个索引中具有值的键_Php_Laravel - Fatal编程技术网

PHP-获取在多个索引中具有值的键

PHP-获取在多个索引中具有值的键,php,laravel,Php,Laravel,我有以下数组: $myArray=[ [ “name”=>null, “价格”=>[ “高度”=>0.0098974902792506, “左”=>0.8385, “页面”=>1, “顶部”=>0.51290208554259, “宽度”=>0.0275, ], ], [ “name”=>null “价格”=>[ “高度”=>0.0098974902792506, “左”=>0.838, “页面”=>1, “顶部”=>0.56981265464829, “宽度”=>0.028, ] ], [ “

我有以下数组:

$myArray=[
[
“name”=>null,
“价格”=>[
“高度”=>0.0098974902792506,
“左”=>0.8385,
“页面”=>1,
“顶部”=>0.51290208554259,
“宽度”=>0.0275,
],
],
[
“name”=>null
“价格”=>[
“高度”=>0.0098974902792506,
“左”=>0.838,
“页面”=>1,
“顶部”=>0.56981265464829,
“宽度”=>0.028,
]
],
[
“name”=>null
“价格”=>[
“高度”=>0.010250972074938,
“左”=>0.5905,
“页面”=>1,
“顶部”=>0.44114528101803,
“宽度”=>0.0285,
]
]
];
我试图检查数组并获取每个数组中都有值(不是
null
)的键的名称。在上面的示例中,这将是
price

但是,阵列也可以如下所示:

[
[
“name”=>null,
“价格”=>[
“高度”=>0.0098974902792506,
“左”=>0.8385,
“页面”=>1,
“顶部”=>0.51290208554259,
“宽度”=>0.0275,
],
],
[
“name”=>null
“价格”=>null
],
[
“name”=>null
“价格”=>null
]
]
在这种情况下,有一个数组键在所有数组中都有一个值

以下是我实现这一目标的尝试:

$originalKeyWithValue=null;
foreach($myArray作为$key=>$item)
{
$originalKeyWithValue=array_key_first($item);
if(isset($myArray[$key+1])){
$nextKeyWithValue=array_key_first($myArray[$key+1]);
如果($originalKeyWithValue!=$nextKeyWithValue){
$originalKeyWithValue=$nextKeyWithValue;
}
} 
}
返回$originalKeyWithValue;

但是,上面的代码返回
name
作为键,即使
null
$myArray

中的所有数组中,我都会这样做:

// 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; 
    }
}

虽然有一个公认的答案,但我想我会分享一种使用Laravel collections实现这一点的方法

 $uniqueKeysWithValues = collect($myArray)->map(function($item){
    return array_keys( collect($item)->filter()->toArray() ); //filter will remove all null
 })->flatten()->unique();

这种方法将为您提供所有包含值的键,即使两个键中都有值。

在您的第一个代码段中,所有名称都为空,算法应该获得哪一个?或者通过
键的名称
您只是指键?在您的代码中,没有任何地方可以测试值是否为
null
,这可以解释它(注意:
array\u key\u first
不测试null值,当数组为空时返回null,这是完全不同的)@Islamelshoboksh它应该得到
price
而不是
name
,因为
price
键在所有数组中都有一个值。如果多个键总是有一个值,该怎么办?如果没有键在所有项中都有值,那么应该返回什么?谢谢!多么优雅、快捷的解决方案啊。这真的教会了我一些东西。非常感谢!非常感谢!