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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:10:59+00:00 2026-06-14T03:10:59+00:00

This should be easy, but I can´t seem to get it to work. I

  • 0

This should be easy, but I can´t seem to get it to work. I have an multidimensional array and I wan´t iterate trough the array and check for a specific value. If the value equals a string then echo out a value of the array.

Here is the array and two values (the full array has a lot more):

$users = array( 
    "username01" => array("fullname" => "Firstname Lastname",
                            "status" => "Online"),
    "username02" => array("fullname" => "Firstname Lastname",
                            "status" => "Offline")
);

I wan’t to echo out the Full name of each user that is “Online”. Here is what I’m using today but it’s not working:

$string = "Online";
foreach ($users as $username => $data) {
    $fullname = $data["fullname"];
    $status   = $data["status"];

    echo $status."= ";
    if ($status == $string) { 
        echo "Yes"; 
    } else { 
        echo "No"; 
    }
      echo "<br>";
}

If I echo out $fullname and $status the correct data is printed out. But for some reason the IF statement is not working. If the user is Offline the echo is “No”, but if the user is Online there is no echo at all.

EDIT – Solved
Updated the array keys with quotes and $data[…] as was suggested below. I found a typo that was causing a false output. Thanks for all the 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-06-14T03:11:00+00:00Added an answer on June 14, 2026 at 3:11 am

    This is working okay for me :

    <?php 
    
        //you forgot to enclose your keys by Single Quote (')
        $users = array( 
        "username01" => array('fullname' => "Firstname Lastname",
                    'status' => "Online"),
        "username02" => array('fullname' => "Firstname Lastname",
                    'status' => "Offline")
    );
    
        $string = "Online";
        foreach ($users as $username => $data) {
          //Notice this line
          //$username is the key 
          //at the first iteraiton it will be uername01
          //$data holds the array of username index in the array named $users
          $fullname = $data["fullname"];
          $status = $data['status'];
          if($status == $string) { echo "Yes"; } else { echo "No"; }
        }
    
    ?>
    

    Go through foreach in php to know more details about foreach.

    Hope that helps. Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK this should be easy but I can't seem to get it to work.
this should be an easy one, but I can't seem to get mysql to
Seems like this should be the easy part, but I can't seem to get
I have a feeling this should be easy but I can't seem to figure
This should be easy to find out but I can't seem to find it
So this should be a real easy question but I can't seem to find
I know this should be easy to find via google, but I can't seem
I'm thinking this should be easy, but I can't seem to figure this out.
This seems like it should be easy as pie, but I can't seem to
This should be easy, but I can't seem to figure it out... How can

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.