Search code examples
phparraysisnullorempty

Check if only some values are not empy in array?


I have some array like this:-

$atts = [
    "images_url" => "1",
    "link_list" => "2",
];

But sometimes array can be like this:-

$atts = [
    "images_url" => "1"
];

Or first can be empty based is it not empty i need some extra logic, how to check if some value of array is not empty or exist and do some extra logic?


Solution

  • you can do it like below:-

    if(count($atts) > count(array_filter($atts))){
      echo "some indexes are empty";
    }
    

    Example:-https://eval.in/728563

    For your another question in comment

    foreach($atts as $key=>$val){
       if(!empty($atts[$key])){ // will check both index exist and have some value
          echo $val;
       }
    }
    

    Output:-https://eval.in/728568