Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Variable in an Array: True or False

General discussions related to php

Moderators: macek, egami, gesf

Variable in an Array: True or False

Postby driscoll » Mon Oct 01, 2012 10:06 am

I am attempting to check to see if a variable is in an array, and if it is echo true if it is not echo false. Not sure what the issue is, but the code below will kick out both true and false for some of the true variables. Please let me know if you need anymore information. Any and all help is appreciated!

Code: Select all
<?php
$page = $_GET['image_id'];
if (!empty($_SESSION)){
   foreach($_SESSION as $name => $value2)   {
      if (substr($name, 0, 7)=='images_') {
      $id = substr($name, 7, (strlen($name)-7));
      $get = mysql_query('SELECT * FROM images WHERE image_id='.mysql_real_escape_string((int)$id));
         while ($cart_info_row = mysql_fetch_assoc($get)) {
            $cart = array('image_id' => $cart_info_row['image_id'],);
            $array = array_values($cart);
            print_r ($array);
            if (in_array($page, $array) == TRUE) { echo 'true';}
         }
      }
      }if (in_array($page, $array) ==FALSE){
      echo 'false';}
   } else echo false;
}   ?>
driscoll
New php-forum User
New php-forum User
 
Posts: 6
Joined: Sun Sep 09, 2012 3:52 pm

Re: Variable in an Array: True or False

Postby seandisanti » Mon Oct 01, 2012 2:13 pm

Code: Select all
echo (isset($array['whatever'])) ? "true" : "false";
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Variable in an Array: True or False

Postby driscoll » Mon Oct 01, 2012 6:10 pm

Thanks for the reply, but unfortunately it has not fixed my issue... I know there is somthing wrong with my logic in the code but cannot figure it out. Please, any further assistance it hugely appreciated. Thanks again.
driscoll
New php-forum User
New php-forum User
 
Posts: 6
Joined: Sun Sep 09, 2012 3:52 pm

Re: Variable in an Array: True or False

Postby seandisanti » Tue Oct 02, 2012 7:53 am

You can use isset() to tell if an element is in an array... i used a ternary operation to demonstrate. the same could be written as...

Code: Select all
if (! isset($arrayname['elementIdentifier']){
   echo "false";
   } else {
   echo "true";
   }
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Variable in an Array: True or False

Postby driscoll » Tue Oct 02, 2012 8:28 am

I believe I understand the function you provided, but I think the in_array is still what I am looking for. My goal is to identify if a variable is in an array, and if it is display a true or false. The variable I am checking is $_GET variable ($page)...

I only want it to either true or false and in some instances I receive both. Below are the two outcomes:

This array is for a True variable (this one is wrong because it is showing both):
Array ( [0] => 43 ) true Array ( [0] => 44 ) Array ( [0] => 46 ) Array ( [0] => 45 ) false

This array is for a False variable:
Array ( [0] => 43 ) Array ( [0] => 44 ) Array ( [0] => 46 ) Array ( [0] => 45 ) false

Again, I just need to get rid of it showing both true and false... Hopefully this makes sense.

Thanks again
driscoll
New php-forum User
New php-forum User
 
Posts: 6
Joined: Sun Sep 09, 2012 3:52 pm

Re: Variable in an Array: True or False

Postby seandisanti » Tue Oct 02, 2012 9:58 am

if it's not in the array, isset() returns false, if it is, it returns true. i thought that's what you wanted?
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Variable in an Array: True or False

Postby driscoll » Tue Oct 02, 2012 10:15 am

Yes, you are correct. But I am receiving a True and False within my function and that is where the problem lies.

For Example:
mypage.com/test.php?image_id=43

$page = $_GET['image_id'];

Array ( [0] => 43 [image_id] => 43 ) = True
Array ( [0] => 44 [image_id] => 44 ) = False
Array ( [0] => 46 [image_id] => 46 ) = False
Array ( [0] => 45 [image_id] => 45 ) = False

My intent is too know if any of these items are True, not to have them all identified on the output.

Hopefully I am making sense, let me know if you need more info.
driscoll
New php-forum User
New php-forum User
 
Posts: 6
Joined: Sun Sep 09, 2012 3:52 pm


Return to PHP General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.