1

Controller

public function category_fetching(){
    $result=$this->AdminModel->category_fetching();
    $arraymaincat=array();   
    $ifarraymatch=""; 
    $result_count=$result[1];  
          for($i=0;$i<$result_count;$i++){
               $uniquemaincat=($result[0][$i])->maincategory;
               if ($arraymaincat=="" && !$uniquemaincat==""){
                  array_push($arraymaincat,$uniquemaincat);
               }
          }
         print_r($arraymaincat);
}

Model

public function category_fetching(){                
            $query=$this->db->get('category');
            $result[0]=$query->result();
            $result[1]=$query->num_rows();
            return $result;
     } 

when I print_r the $arraymaincat it gives an empty array, in the log I get

Array
(
)
Vickel
  • 7,879
  • 6
  • 35
  • 56
Anvin
  • 49
  • 8
  • ensure that code satisfies the loop as well as if condition. otherwise it will return empty array – Ramki Dec 12 '19 at 11:57
  • both loop and if conditions are working . whe i print $result_count i get 10, whe i print $uniquemaincat i get 'clothing'. is there any probelm with the scope of the $arraymaincat , is the changes to $arramaincat not applicable outside the if condition and loop ? – Anvin Dec 12 '19 at 12:02
  • You have `$arraymaincat==""` - as `$arraymaincat` is an array, not sure what you are trying to achieve with this. – Nigel Ren Dec 12 '19 at 12:05
  • @Nigel Ren if there is no element in array then push the value to array – Anvin Dec 12 '19 at 12:12
  • 1
    An empty array isn't = `""` - try `$arraymaincat==[]` or use `empty($arraymaincat)` – Nigel Ren Dec 12 '19 at 12:15
  • @ Nigel Ren thank-you bro, empty($arraymaincat) worked – Anvin Dec 12 '19 at 12:19

1 Answers1

0

You can also try if (count($arraymaincat) == 0 && count($uniquemaincat) != 0).

Romi Halasz
  • 1,949
  • 1
  • 13
  • 23