Ad

Laravel Array Helpers If Array Has A Value

- 1 answer

I have an array has below returned from my blade :

Array ( 
[0] => Array ( [AddOns] => Array ( [name] => Icann [type] => AddOns [value] => +20.00 [attributes] => Array ( [description] => Icann Fee ) ) ) 
[1] => Array ( [Package] => Array ( [name] => Domain [type] => Package [value] => +0.00 [attributes] => Array ( [description] => Domain Registration ) ) ) )

how can i check if value Icann in AddOns['name'] exists ie AddOns['name']=Icann and get its description ie AddOns['attributes']['description'] in my blade template ?

Ad

Answer

FROM QUESTION AUTHOR

I realized that array_has() did not acctually get the required value so after searching i created a contract/interface with below method validator and used @inject in blade to access the interface class as below :

method in interface

public  function search_array($needle, $haystack) {

 if(in_array($needle, $haystack)) {
      return true;
 }
 foreach($haystack as $element) {
      if(is_array($element) && $this->search_array($needle, $element))
           return true;
 }
 return false;
} 

blade template

  @inject('searchicann', 'App\Helpers\Contracts\RocketShipContract')
  @if ($searchicann->search_array('Icann', $array ))
     Found
  @endif
Ad
source: stackoverflow.com
Ad