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 546879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:54:48+00:00 2026-05-13T10:54:48+00:00

$headers=array( $requestMethod. /rest/obj HTTP/1.1, listable-meta: .$listablemeta, meta: .$nonlistmeta, ‘accept: */*’, ); In the above

  • 0
$headers=array(
     $requestMethod." /rest/obj HTTP/1.1",
     "listable-meta: ".$listablemeta,
     "meta: ".$nonlistmeta,
     'accept: */*',
      );

In the above example, I’d like to omit the whole line if $listablemeta or $nonlistmeta is blank.
Assume $listablemeta is blank. Then the array would be:

$headers=array(
     $requestMethod." /rest/obj HTTP/1.1",
     "meta: ".$nonlistmeta,
     'accept: */*',
      );

Now I can setup a conditional isempty() and set the array accordingly, but what if I want to construct an array with say 20 different values each only setting if the variable on each line is not empty, is there another way to set a conditional -within- an array declaration? If not, what is another way to approach this problem?

Thanks!

  • 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-05-13T10:54:48+00:00Added an answer on May 13, 2026 at 10:54 am

    You can’t do any conditionals inside the array clause that would help you with this, but this should suit your needs:

    If the headers you want to pass to the array are as follows:

    $requestMethod = 'GET';
    $listablemeta = ''; // This shouldn't be in the final result
    $nonlistmeta = 'non-listable-meta';
    

    Build a key/value array of these variables:

    $headers = array(
                   0 => $requestMethod." /rest/obj HTTP/1.1",
                   'listable-meta' => $listablemeta,
                   'meta' => $nonlistmeta,
                   'accept', '*/*'
               );
    

    Note that if the value doesn’t have a key as in requestMethod, just put a numeric value there. Then loop them through and build the final array:

    function buildHeaders($headers) {
        $new = array();
    
        foreach($headers as $key => $value) {
            // If value is empty, skip it
            if(empty($value)) continue;
            // If the key is numerical, don't print it
            $new[] = (is_numeric($key) ? '' : $key.': ').$value;
        }
    
        return $new;
    }
    
    $headers = buildHeaders($headers);
    

    $headers should now contain something like this:

    $headers = array(
                   'GET /rest/obj HTTP/1.1',
                   'meta: non-listable-meta-here',
                   'accept: */*'
               );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

headers : <script type=text/javascript src=http://code.jquery.com/jquery-1.7.2.min.js></script> <script type=text/javascript src=js/timeago.js ></script> <script src=js/test_helpers.js type=text/javascript></script> photos.php $(.cmdclose).click(function(){
Are there any HTTP headers one can use to detect a mobile users number
So I decided to use digest HTTP authentication for my REST API. I googled
My code looks like this: val people = Array(Array(John, 25), Array(Mary, 22)) val headers
I use php function file_get_contents and one of params is http headers. I make
In Zend Framework in Response Class there are two different arrays for storing headers:
The headers for libbz2.dylib on the iPhone are missing, or contained in a less
I have set cache headers to be far in future (1 year from now)
Pre-compiled headers seem like they can save a lot of time in large projects,
Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =

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.