1

I have the following method where you pass a list of items in, and the first item is what you want to see if it exists, and the following items are the path to the item.

In the following I have 2 print_r statements, one before the for and one after it.

public function exists(){
    $keys  = func_get_args();
    $value = array_shift($keys);

    $ref = &$_SESSION;
    print_r($_SESSION);
    for($x = 0; $x < sizeof($keys); $x++){
        $ref = &$ref[$keys[$x]];
    }
    print_r($_SESSION);
    if(!is_array($ref)){
        unset($ref);
        return false;
    }
    $found = in_array($value, $ref);
    unset($ref);
    return $found;
}

and when I call it like this:

$obj->exists(123, "cart");

I get these two arrays from those print_r's:

Array
(
    [id] => 1
    [email] => [email protected]
    [user] => TheColorRed
    [first] => Billy
    [last] => Bob
    [ZingLoggedIn] => 1
)
Array
(
    [id] => 1
    [email] => [email protected]
    [user] => TheColorRed
    [first] => Billy
    [last] => Bob
    [ZingLoggedIn] => 1
    [cart] => 
)

My question is, why is it adding cart to the array? It should only be checking to see if it exists.

5
  • Try to enable E_ALL error reporting level and check your logs first. PS: your unset is completely redundant there. Commented Jan 20, 2015 at 0:03
  • Interesting. Wouldn't an undefined offset notice be raised in the for loop? Maybe do an isset check and break if it returns false. Commented Jan 20, 2015 at 0:03
  • @zerkms I have error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT and display_errors = On Commented Jan 20, 2015 at 0:11
  • @The Boogie Man: and you don't get notices/warnings for this code? Commented Jan 20, 2015 at 0:11
  • nope I get no errors and/or warnings Commented Jan 20, 2015 at 0:12

1 Answer 1

2

This is the side effect of using references on array elements; if the element doesn't exist it gets created. The unset() afterwards doesn't change that. Consider not using references; since you're only reading the values, there should be no copy-on-write taking place:

public function exists()
{
    $keys  = func_get_args();
    $value = array_shift($keys);

    $ref = $_SESSION;
    foreach ($keys as $key) {
        if (!isset($ref[$key])) {
            return false;
        }
        $ref = $ref[$key];
    }
    return is_array($ref) && in_array($value, $ref);
}
Sign up to request clarification or add additional context in comments.

7 Comments

will this work for $obj->exists("cat", "item1", "item2") -> $array['item1']['item2']?
@The Boogie Man: what if you try before you ask?
@TheBoogieMan It won't create array elements; with a function called exists() I wouldn't expect that kind of side effect anyway.
@Ja͢ck but why don't you use isset as well?
I added && array_key_exists($key, $ref) to the if statement because it was saying the index "cart" doesn't exist. Is it a good idea to add that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.