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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:58:07+00:00 2026-05-27T18:58:07+00:00

I have a variable that I got out of the database table like this:

  • 0

I have a variable that I got out of the database table like this:

while($r = mysql_fetch_array($query) //The query is just: SELECT * FROM stuff_table WHERE userid='$userid'
{ 
$stuff = $r['stuff'];
}

The stuff variable looks like this: “cars, books, computers, foods” and I made it an array:

//I used:
$array_of_stuff = explode(",", $stuff); //This gave me an array.

Now I wanted to check its values, like this:

if(in_array("cars", $array_of_stuff) && in_array("books", $array_of_stuff))
{ 

//This line is the problem, I want it to check to see if it has "cars" and "books" in the array but this code is not working for that like it should. Instead of checking and finding both, it just goes on to the next elseif. 

echo "Cars and Books";
} elseif(in_array("cars")) {
//
echo "Only cars";
} elseif(in_array("books")) {
echo "Only books";
} else {
echo "Other stuff...";
}

The output I get from this is: “Only cars” instead of “Cars and Books”.

So how can I make my code check the array for 2 or more values before continuing its if/elseif statement? Is it possible with the in_array function?

Var Dump:

array(4) { [0]=> string(4) "cars" [1]=> string(6) " books" [2]=> string(10) " computers" [3]=> string(6) " foods" }
  • 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-27T18:58:08+00:00Added an answer on May 27, 2026 at 6:58 pm

    If $array_of_stuff is actually set to explode(",","cars, books, computers, foods") as you described then the array will have leading spaces before the values other than cars. The in_array() function doesn’t match substrings of the target elements, so “books” will not match against ” books” in the array.

    If you need to parse a string like that, you’ll need to make sure you have an exact definition of the format it’s in so you can correctly parse it into an array. If “, ” is the only delimiter and that sequence always represents a delimiter (it’s not ever escaped or quoted, for example), then you could change the explode(",",...) to explode(", ",...). If this is data you’re storing, be aware that you shouldn’t invent your own format. Instead, use something like serialize() (which only PHP understands) or json_encode() (which is highly portable) to convert native types into a string (and the corresponding decode functions to decode it).

    Beyond that, storing an array in a field of a row of a SQL table is often, but not always, a sign that the schema is wrong.

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

Sidebar

Related Questions

I've got an sql query that pulls locations from a database based on coordinates
I have a variable that is built in loop. Something like: $str = ;
I have a variable that contains a 4 byte, network-order IPv4 address (this was
I have a variable that has this string: <DIV><SPAN style=FONT-FAMILY: Tahoma; FONT-SIZE: 10pt>[If the
If I have a variable that contains text information (say taken from a textarea),
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I have a variable that im define with another variable and some text. $title
I have a variable that can either contain a list of strings or a
I have a variable that contains : Sometimes array of strings and sometimes array

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.