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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:30:07+00:00 2026-05-23T19:30:07+00:00

I have a question about arrays and foreach. If i have an array like

  • 0

I have a question about arrays and foreach.

If i have an array like this:

$test_arr = array();
$test_arr['name1'] = "an example sentence";
$test_arr['anything'] = "dsfasfasgsdfg";
$test_arr['code'] = "4334refwewe";
$test_arr['empty1'] = "";
$test_arr['3242'] = "";

how can I do a foreach and “pick” only the ones that have values? (in my array example, would only take the first 3 ones, name1, anything and code).

I tried with

foreach ($test_arr as $test) {
  if (strlen($test >= 1)) {
    echo $test . "<br>";
  }
}

but it doesn’t work. Without the “if” condition it works, but empty array values are taken into consideration and I don’t want that (because I need to do a <br> after each value and I don’t want a <br> if there is no value)

Sorry if I don’t explain myself very well, I hope you understand my point. Shouldn’t be too difficult I guess..

Thanks for your help !

  • 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-23T19:30:07+00:00Added an answer on May 23, 2026 at 7:30 pm

    Maybe will work

    foreach ($test_arr as $test) {
      if (strlen($test)!=="") {
        echo $test . "<br>";
      }
    }
    

    Your solution with corrected syntax:

    foreach ($test_arr as $test) {
      if (strlen($test)>=1) {
        echo $test . "<br>";
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry about the naive question. I have the following: public Array Values {
I have a question about locking. This doesn't have to be only about record
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but
I am writing a script in Perl and have a question about Perl's foreach
This is a question about returning efficiently strings and chars from a string array
I have a question about efficient implementation. Lets say I have two arrays: One
I have a question about Ruby arrays but it's kind of hard to describe
I have an XML structure that looks like this: <?xml version=1.0?> <survey> <responses>0</responses> <question>
I have a question about using variables as variablename. I have an array with

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.