0

I have the below array called $items:

array:3 [▼
  0 => array:9 [▼
    "qty" => 1
    "name" => "Guide, interlocked slats, R Commodity code: 39239000 / Country of origin: PL. Delivery note 838174147 from 12.09.2019 PC 1,50/ 10"
  ]
  1 => array:9 [▼
    "qty" => null
    "name" => "Guide, interlocked slats, L Commodity code: 39239000 / Country of origin: PL. Delivery note 838174147 from 12.09.2019 PC"
  ]
  2 => array:9 [▼
    "qty" => null
    "name" => "Bottom groove set L + R Commodity code: 39239000 / Country of origin:"
  ]
]

I am trying to find the specific key, that has a value in all three subarray. In this case, this would be the key name, as this specific key has a value in all three arrays.

I have tried to write a function for this, as you can see below:

function getKeysWithData(array $items): array
{
    //Get the key(s) that has region data for all items.
    $keysWithData = collect($items)->map(function ($item) {
        return array_keys(collect($item)->filter()->toArray()); //filter will remove all null
    })->flatten()->unique()->toArray();
   
}

The above function returns an array, containing the name of the keys that has some values. So for the above $items, it will return:

array:2 [▼
  0 => "qty"
  1 => "name"
]

This is because both qty and name contain some value at some point. However, it should only return name.

How can I do, so it will only return the name of the key(s), that has data in all arrays?

2
  • Try this ……unique()->only(['name'])->toArray(); Commented Aug 23, 2020 at 17:57
  • The problem is that I don’t know the name of the key beforehand (it’s dynamic names, can be anything) Commented Aug 23, 2020 at 17:58

2 Answers 2

1

Though this is not laravel and all this functional style, but at least it loops over your array only once:

// Take first element so as to know what keys do we have:
$keys = $items[0];
foreach ($items as $item) {
    foreach ($item as $key => $value) {
        if ($value === null) {
            // unset the key which has NULL value
            unset($keys[$key]);
        }
        
        // if there no keys left - break all loops
        if (empty($keys)) {
            break 2;
        }
    }
}
print_r(array_keys($keys));

And da fiddle.

Sign up to request clarification or add additional context in comments.

Comments

0

Use two times foreach loop for two dimension array. ex: '''

       foreach($array as $k=>$v){
        //$k is 0
        //$v contains another inside array so use another foreach loop
         foreach($v as $x => $y){
    
       if($y != "null" && $x != "null" && $x == "name"){
    echo $x;  //it contains key ex : name
    echo $y;  //it contains value ex : 1
   $z[$x]=$y; // it contains only name key
    }
          
        }
        
        }

'''

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.