How to call if condition if only one array is empty if both empty then call else condition?












0















I am using CodeIgniter. I am getting the response from model and display on the controller.



Model



//some code here
return array('secondary_data'=> $result,'primary_data' =>$result2);// sending to controller
// some code here


Controller



$result=$this->Search_model->get_search_name($cust_name);//calling model    
$data['secondary_data'] = $result['secondary_data'];
$data['primary_data'] = $result['primary_data'];

print_r($data['secondary_data']);
print_r($data['primary_data']);


output



Array
(
[0] => stdClass Object
(
[member_id] =>
[customer_id] =>
[first_name] => qwer
[last_name] => poiu
[email] =>
[member_type] => 2

)

)
Array
(
[0] => stdClass Object
(
[member_id] => 1
[customer_id] => 0011
[first_name] => asdasd
[last_name] => asdasda
[email] => bappyang68@gmail.com
[member_type] => 1

)

)


but sometimes I am getting empty array of print_r($data['primary_data']) because of no data. Yes, it's possible.;



but in below if condition I am checking the data count count($data) > 0 and if found empty then calling the else part.



I don't want to call else part if only one array is emply. if both array is empty then it should call else part



    if (count($data) > 0)
{
$data['title'] = "Search";
$data['heading'] = "Search";
$data['content'] = $this->load->view('search',$data,true);
$this->load->view('dashboard/dashboard',$data);
}
else{
echo "NO data available";
}


Any idea how to do this? Thanks










share|improve this question























  • if(count($data) && (!empty($data['secondary_data']) || !empty($data['primary_data']))){...}else{...}

    – suresh bambhaniya
    Nov 16 '18 at 6:15













  • write condition for both array or Use loop and check

    – Bhargav Chudasama
    Nov 16 '18 at 6:16
















0















I am using CodeIgniter. I am getting the response from model and display on the controller.



Model



//some code here
return array('secondary_data'=> $result,'primary_data' =>$result2);// sending to controller
// some code here


Controller



$result=$this->Search_model->get_search_name($cust_name);//calling model    
$data['secondary_data'] = $result['secondary_data'];
$data['primary_data'] = $result['primary_data'];

print_r($data['secondary_data']);
print_r($data['primary_data']);


output



Array
(
[0] => stdClass Object
(
[member_id] =>
[customer_id] =>
[first_name] => qwer
[last_name] => poiu
[email] =>
[member_type] => 2

)

)
Array
(
[0] => stdClass Object
(
[member_id] => 1
[customer_id] => 0011
[first_name] => asdasd
[last_name] => asdasda
[email] => bappyang68@gmail.com
[member_type] => 1

)

)


but sometimes I am getting empty array of print_r($data['primary_data']) because of no data. Yes, it's possible.;



but in below if condition I am checking the data count count($data) > 0 and if found empty then calling the else part.



I don't want to call else part if only one array is emply. if both array is empty then it should call else part



    if (count($data) > 0)
{
$data['title'] = "Search";
$data['heading'] = "Search";
$data['content'] = $this->load->view('search',$data,true);
$this->load->view('dashboard/dashboard',$data);
}
else{
echo "NO data available";
}


Any idea how to do this? Thanks










share|improve this question























  • if(count($data) && (!empty($data['secondary_data']) || !empty($data['primary_data']))){...}else{...}

    – suresh bambhaniya
    Nov 16 '18 at 6:15













  • write condition for both array or Use loop and check

    – Bhargav Chudasama
    Nov 16 '18 at 6:16














0












0








0








I am using CodeIgniter. I am getting the response from model and display on the controller.



Model



//some code here
return array('secondary_data'=> $result,'primary_data' =>$result2);// sending to controller
// some code here


Controller



$result=$this->Search_model->get_search_name($cust_name);//calling model    
$data['secondary_data'] = $result['secondary_data'];
$data['primary_data'] = $result['primary_data'];

print_r($data['secondary_data']);
print_r($data['primary_data']);


output



Array
(
[0] => stdClass Object
(
[member_id] =>
[customer_id] =>
[first_name] => qwer
[last_name] => poiu
[email] =>
[member_type] => 2

)

)
Array
(
[0] => stdClass Object
(
[member_id] => 1
[customer_id] => 0011
[first_name] => asdasd
[last_name] => asdasda
[email] => bappyang68@gmail.com
[member_type] => 1

)

)


but sometimes I am getting empty array of print_r($data['primary_data']) because of no data. Yes, it's possible.;



but in below if condition I am checking the data count count($data) > 0 and if found empty then calling the else part.



I don't want to call else part if only one array is emply. if both array is empty then it should call else part



    if (count($data) > 0)
{
$data['title'] = "Search";
$data['heading'] = "Search";
$data['content'] = $this->load->view('search',$data,true);
$this->load->view('dashboard/dashboard',$data);
}
else{
echo "NO data available";
}


Any idea how to do this? Thanks










share|improve this question














I am using CodeIgniter. I am getting the response from model and display on the controller.



Model



//some code here
return array('secondary_data'=> $result,'primary_data' =>$result2);// sending to controller
// some code here


Controller



$result=$this->Search_model->get_search_name($cust_name);//calling model    
$data['secondary_data'] = $result['secondary_data'];
$data['primary_data'] = $result['primary_data'];

print_r($data['secondary_data']);
print_r($data['primary_data']);


output



Array
(
[0] => stdClass Object
(
[member_id] =>
[customer_id] =>
[first_name] => qwer
[last_name] => poiu
[email] =>
[member_type] => 2

)

)
Array
(
[0] => stdClass Object
(
[member_id] => 1
[customer_id] => 0011
[first_name] => asdasd
[last_name] => asdasda
[email] => bappyang68@gmail.com
[member_type] => 1

)

)


but sometimes I am getting empty array of print_r($data['primary_data']) because of no data. Yes, it's possible.;



but in below if condition I am checking the data count count($data) > 0 and if found empty then calling the else part.



I don't want to call else part if only one array is emply. if both array is empty then it should call else part



    if (count($data) > 0)
{
$data['title'] = "Search";
$data['heading'] = "Search";
$data['content'] = $this->load->view('search',$data,true);
$this->load->view('dashboard/dashboard',$data);
}
else{
echo "NO data available";
}


Any idea how to do this? Thanks







php codeigniter-3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 6:11









user9437856user9437856

481314




481314













  • if(count($data) && (!empty($data['secondary_data']) || !empty($data['primary_data']))){...}else{...}

    – suresh bambhaniya
    Nov 16 '18 at 6:15













  • write condition for both array or Use loop and check

    – Bhargav Chudasama
    Nov 16 '18 at 6:16



















  • if(count($data) && (!empty($data['secondary_data']) || !empty($data['primary_data']))){...}else{...}

    – suresh bambhaniya
    Nov 16 '18 at 6:15













  • write condition for both array or Use loop and check

    – Bhargav Chudasama
    Nov 16 '18 at 6:16

















if(count($data) && (!empty($data['secondary_data']) || !empty($data['primary_data']))){...}else{...}

– suresh bambhaniya
Nov 16 '18 at 6:15







if(count($data) && (!empty($data['secondary_data']) || !empty($data['primary_data']))){...}else{...}

– suresh bambhaniya
Nov 16 '18 at 6:15















write condition for both array or Use loop and check

– Bhargav Chudasama
Nov 16 '18 at 6:16





write condition for both array or Use loop and check

– Bhargav Chudasama
Nov 16 '18 at 6:16












4 Answers
4






active

oldest

votes


















2














Check your both array separately in or condition



if (count($data['primary_data']) > 0 || count($data['secondary_data']) > 0)
{
$data['title'] = "Search";
$data['heading'] = "Search";
$data['content'] = $this->load->view('search',$data,true);
$this->load->view('dashboard/dashboard',$data);
}else{
echo "NO data available";
}





share|improve this answer

































    1














    Try this condition with OR operator



    if(!empty($data['primary_data']) || !empty($data['secondary_data'])){
    //One or both array have values
    }else{
    //Both have no values
    }





    share|improve this answer
























    • Your code is also working for me. upvote from my side

      – user9437856
      Nov 16 '18 at 7:51






    • 1





      This is short and simple solution.

      – Smartpal
      Nov 16 '18 at 7:52



















    0














    You can use separate condition for both array, If one of them is not empty then apply If condition otherwise else.



    if ( !empty($data['secondary_data']) || !empty($data['primary_data']) ) {
    $data['title'] = "Search";
    $data['heading'] = "Search";
    $data['content'] = $this->load->view('search',$data,true);
    $this->load->view('dashboard/dashboard',$data);
    } else {
    echo "NO data available";
    }





    share|improve this answer































      0














      start checking from $data then sub array



      if(count($data) && (isset($data['secondary_data']) && !empty($data['secondary_data'])) || (isset($data['primary_data']) && !empty($data['primary_data']))){
      ....
      }else{
      ...
      }





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332375%2fhow-to-call-if-condition-if-only-one-array-is-empty-if-both-empty-then-call-else%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        Check your both array separately in or condition



        if (count($data['primary_data']) > 0 || count($data['secondary_data']) > 0)
        {
        $data['title'] = "Search";
        $data['heading'] = "Search";
        $data['content'] = $this->load->view('search',$data,true);
        $this->load->view('dashboard/dashboard',$data);
        }else{
        echo "NO data available";
        }





        share|improve this answer






























          2














          Check your both array separately in or condition



          if (count($data['primary_data']) > 0 || count($data['secondary_data']) > 0)
          {
          $data['title'] = "Search";
          $data['heading'] = "Search";
          $data['content'] = $this->load->view('search',$data,true);
          $this->load->view('dashboard/dashboard',$data);
          }else{
          echo "NO data available";
          }





          share|improve this answer




























            2












            2








            2







            Check your both array separately in or condition



            if (count($data['primary_data']) > 0 || count($data['secondary_data']) > 0)
            {
            $data['title'] = "Search";
            $data['heading'] = "Search";
            $data['content'] = $this->load->view('search',$data,true);
            $this->load->view('dashboard/dashboard',$data);
            }else{
            echo "NO data available";
            }





            share|improve this answer















            Check your both array separately in or condition



            if (count($data['primary_data']) > 0 || count($data['secondary_data']) > 0)
            {
            $data['title'] = "Search";
            $data['heading'] = "Search";
            $data['content'] = $this->load->view('search',$data,true);
            $this->load->view('dashboard/dashboard',$data);
            }else{
            echo "NO data available";
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 6:20

























            answered Nov 16 '18 at 6:15









            SachinSachin

            610412




            610412

























                1














                Try this condition with OR operator



                if(!empty($data['primary_data']) || !empty($data['secondary_data'])){
                //One or both array have values
                }else{
                //Both have no values
                }





                share|improve this answer
























                • Your code is also working for me. upvote from my side

                  – user9437856
                  Nov 16 '18 at 7:51






                • 1





                  This is short and simple solution.

                  – Smartpal
                  Nov 16 '18 at 7:52
















                1














                Try this condition with OR operator



                if(!empty($data['primary_data']) || !empty($data['secondary_data'])){
                //One or both array have values
                }else{
                //Both have no values
                }





                share|improve this answer
























                • Your code is also working for me. upvote from my side

                  – user9437856
                  Nov 16 '18 at 7:51






                • 1





                  This is short and simple solution.

                  – Smartpal
                  Nov 16 '18 at 7:52














                1












                1








                1







                Try this condition with OR operator



                if(!empty($data['primary_data']) || !empty($data['secondary_data'])){
                //One or both array have values
                }else{
                //Both have no values
                }





                share|improve this answer













                Try this condition with OR operator



                if(!empty($data['primary_data']) || !empty($data['secondary_data'])){
                //One or both array have values
                }else{
                //Both have no values
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 6:18









                SmartpalSmartpal

                982517




                982517













                • Your code is also working for me. upvote from my side

                  – user9437856
                  Nov 16 '18 at 7:51






                • 1





                  This is short and simple solution.

                  – Smartpal
                  Nov 16 '18 at 7:52



















                • Your code is also working for me. upvote from my side

                  – user9437856
                  Nov 16 '18 at 7:51






                • 1





                  This is short and simple solution.

                  – Smartpal
                  Nov 16 '18 at 7:52

















                Your code is also working for me. upvote from my side

                – user9437856
                Nov 16 '18 at 7:51





                Your code is also working for me. upvote from my side

                – user9437856
                Nov 16 '18 at 7:51




                1




                1





                This is short and simple solution.

                – Smartpal
                Nov 16 '18 at 7:52





                This is short and simple solution.

                – Smartpal
                Nov 16 '18 at 7:52











                0














                You can use separate condition for both array, If one of them is not empty then apply If condition otherwise else.



                if ( !empty($data['secondary_data']) || !empty($data['primary_data']) ) {
                $data['title'] = "Search";
                $data['heading'] = "Search";
                $data['content'] = $this->load->view('search',$data,true);
                $this->load->view('dashboard/dashboard',$data);
                } else {
                echo "NO data available";
                }





                share|improve this answer




























                  0














                  You can use separate condition for both array, If one of them is not empty then apply If condition otherwise else.



                  if ( !empty($data['secondary_data']) || !empty($data['primary_data']) ) {
                  $data['title'] = "Search";
                  $data['heading'] = "Search";
                  $data['content'] = $this->load->view('search',$data,true);
                  $this->load->view('dashboard/dashboard',$data);
                  } else {
                  echo "NO data available";
                  }





                  share|improve this answer


























                    0












                    0








                    0







                    You can use separate condition for both array, If one of them is not empty then apply If condition otherwise else.



                    if ( !empty($data['secondary_data']) || !empty($data['primary_data']) ) {
                    $data['title'] = "Search";
                    $data['heading'] = "Search";
                    $data['content'] = $this->load->view('search',$data,true);
                    $this->load->view('dashboard/dashboard',$data);
                    } else {
                    echo "NO data available";
                    }





                    share|improve this answer













                    You can use separate condition for both array, If one of them is not empty then apply If condition otherwise else.



                    if ( !empty($data['secondary_data']) || !empty($data['primary_data']) ) {
                    $data['title'] = "Search";
                    $data['heading'] = "Search";
                    $data['content'] = $this->load->view('search',$data,true);
                    $this->load->view('dashboard/dashboard',$data);
                    } else {
                    echo "NO data available";
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 6:18









                    Gajanan KolpukeGajanan Kolpuke

                    9910




                    9910























                        0














                        start checking from $data then sub array



                        if(count($data) && (isset($data['secondary_data']) && !empty($data['secondary_data'])) || (isset($data['primary_data']) && !empty($data['primary_data']))){
                        ....
                        }else{
                        ...
                        }





                        share|improve this answer




























                          0














                          start checking from $data then sub array



                          if(count($data) && (isset($data['secondary_data']) && !empty($data['secondary_data'])) || (isset($data['primary_data']) && !empty($data['primary_data']))){
                          ....
                          }else{
                          ...
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            start checking from $data then sub array



                            if(count($data) && (isset($data['secondary_data']) && !empty($data['secondary_data'])) || (isset($data['primary_data']) && !empty($data['primary_data']))){
                            ....
                            }else{
                            ...
                            }





                            share|improve this answer













                            start checking from $data then sub array



                            if(count($data) && (isset($data['secondary_data']) && !empty($data['secondary_data'])) || (isset($data['primary_data']) && !empty($data['primary_data']))){
                            ....
                            }else{
                            ...
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 6:25









                            suresh bambhaniyasuresh bambhaniya

                            945415




                            945415






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332375%2fhow-to-call-if-condition-if-only-one-array-is-empty-if-both-empty-then-call-else%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine