简体   繁体   中英

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. In the above example, this would be 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.

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 .

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.

 $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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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