Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:31:53+00:00 2026-06-14T07:31:53+00:00

I need to pass php array to jquery for some tasks. The php array

  • 0

I need to pass php array to jquery for some tasks. The php array is created for SESSION and then json_encoded for jquery. After that I’ll store that variable to window js namespace in order to use the array in my jquery script.

if(isset($_SESSION['mySession'])){
    $json_array = json_encode($php_array);
}

<script>
    <?php echo "window.json_from_php = ".$json_array; ?>
</script>

The thing i need to do is to check whether this array exist at all/is empty/undefined before doing anything. The check clause succeeds for ‘undefined’ operand but not for the rest. How can I check in jquery whether array is empty or not?

This is the output of the json_encoded array after initialization, when there isn’t any elements in the $php_array:

string '[""]' (length=4)

And here is the jquery:

$(function() {
        var results = $('#results');
        var js_array = window.json_from_php;
        var count = js_array.length;

        if ((typeof window.json_from_php === 'undefined') || (js_array === null) || (jQuery.isEmptyObject(js_array)) || (count === 0)) {
            $('<p>Array is empty.</p>').appendTo(results);
        }
        else {
            jQuery.each(js_array,function(key,val){
                $('<ul><li id="'+key+'">'+val+'</li></ul>').appendTo(results);                  
            });
        }
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:31:54+00:00Added an answer on June 14, 2026 at 7:31 am

    This is how I check for an empty/null json array.

    if (js_array == undefined || js_array == null || js_array.length == 0){
        $('<p>Array is empty.</p>').appendTo(results);
    }
    

    If your json array is [""] then you may need to add something like

    if (js_array == undefined || js_array == null || js_array.length == 0 || (js_array.length == 1 && js_array[0] == ""))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pass an array of numeric values from jQuery to a php
I need to pass a variable which is captured from client side via jquery
I have several situations where I need to pass multi-dimensional PHP arrays into Javascript/jQuery.
I'm doing something which I need to pass an array from my php to
I have a more dimensional php array that i want to pass to a
I have jQuery Ajax request that sends data to a PHP page that then
I'm trying to pass an array through jquery ajax call. However, I need to
I am working on a Wordpress plugin and I need to pass PHP array
The regular expression ^/wp-[^./]+\.php$ matches: wp-config.php wp-commentsrss2.php wp-pass.php wp-rdf.php [...] But I need another
This question is regarding getopt function in php. I need to pass two parameter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.