简体   繁体   English

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

[英]PHP - Get the key that has a value in multiple indexes

I have the below array:我有以下数组:

$myArray = [
   [
     "name" => null,
     "price" => [
         "height" => 0.0098974902792506,
         "left" => 0.8385,
         "page" => 1,
         "top" => 0.51290208554259,
         "width" => 0.0275,
     ],
   ],
   [
     "name" => null
     "price" => [
         "height" => 0.0098974902792506,
         "left" => 0.838,
         "page" => 1,
         "top" => 0.56981265464829,
         "width" => 0.028,
     ]
   ],
   [
     "name" => null
     "price" => [
         "height" => 0.010250972074938,
         "left" => 0.5905,
         "page" => 1,
         "top" => 0.44114528101803,
         "width" => 0.0285,
     ]
   ]
];

I am trying to check the array and get the name of the key that has a value (is not null ) in each array.我正在尝试检查数组并获取每个数组中具有值(不是null )的的名称。 In the above example, this would be price .在上面的示例中,这将是price

However, the array could also look like this:但是,数组也可能如下所示:

[
   [
     "name" => null,
     "price" => [
         "height" => 0.0098974902792506,
         "left" => 0.8385,
         "page" => 1,
         "top" => 0.51290208554259,
         "width" => 0.0275,
     ],
   ],
   [
     "name" => null
     "price" => null
   ],
   [
     "name" => null
     "price" => null
   ]
]

In this case, there is not an array key that has a value in all of the arrays.在这种情况下,没有一个数组键在所有 arrays 中都有值。

Below is my attempt to achieve this:以下是我实现这一目标的尝试:

$originalKeyWithValue = null;
foreach($myArray as $key => $item)
{

  $originalKeyWithValue = array_key_first($item);
  if (isset($myArray[$key+1])) {
    $nextKeyWithValue = array_key_first($myArray[$key+1]);
  
    if($originalKeyWithValue != $nextKeyWithValue){
        $originalKeyWithValue = $nextKeyWithValue;
    }
    
  } 
}
return $originalKeyWithValue;

However the code above returns name as the key, even though it is null in all of the arrays in $myArray .然而,上面的代码返回name作为键,即使它在$myArray中的所有null中都是 null 。

This is what would I do:这就是我要做的:

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

Although there is an accepted answer, I thought I would share a way to do this using Laravel collections.尽管有一个公认的答案,但我想我会分享一种使用 Laravel collections 的方法。

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

This approach will give you all keys that has values in it, even if there are values in both keys.这种方法将为您提供所有具有值的键,即使两个键中都有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM