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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 297k
  • Answers 297k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You use a hashtable to store currently discovered keys (access… May 13, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer The problem was with the Suhosin PHP hardening module. Some… May 13, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer Have you looked at thee articles? might be of some… May 13, 2026 at 7:21 pm

Related Questions

Working on a project in AS3 I require to stream a few files for
I've created an Extension of GenericHandler called SOAPHeaderHandler. I placed log4j statements in the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.