3

I have an array that goes like this

$array = array('key1' => $_GET['value1'], 'key2' => $_GET['value2']);

I want to check if the key's value is not empty. Say a user goes to the webpage and requests this http://mysite.com/page.php?value1=somevalue&value2= which means that value2 is empty or say that it is missing from the query string i want to be able to check if it's there and is not empty.

I tried this

foreach($array as $key => $value)
{
    if(empty($value))
    {
        //spout some error
    }
}

but even though i enter this http://mysite.com/page.php?value1=somevalue meaning that value2 is missing, i don't get the error.

How do i achieve what i want?

dikidera
  • 2,004
  • 6
  • 29
  • 36
  • 2
    [I don't believe you](http://codepad.org/u85hIz4x) that your code does not work. Are you sure that you haven't made an error somewhere else? Also, please accept answers to some of your previous questions; this is a community, not a free-for-all. Five questions and no accepts is absolutely not OK! – Lightness Races in Orbit Mar 29 '11 at 22:54
  • I never knew i had to accept something..i don't even know what to accept. – dikidera Mar 29 '11 at 22:58
  • @mcfe - When you have an answer that "answers" your question, click the checkmark outlined to the left of the answer you accept. It's a courtesy. – Jared Farrish Mar 29 '11 at 23:00
  • @mcfe: Click the "FAQ" link that can be found at the top of every page and have a read! Welcome to Stack Overflow. – Lightness Races in Orbit Mar 29 '11 at 23:01
  • There is no check mark anywhere on this page EDIT: Now it appeared..there was a star instead of a checkmark outline – dikidera Mar 29 '11 at 23:03
  • Checkmark, not checkbox. It's the outlined checkmark below the two green checkmarks here: http://sstatic.net/stackoverflow/img/sprites.png?v=3 – Jared Farrish Mar 29 '11 at 23:05

5 Answers5

2

When I do this:

<?php 

$array = array('key1' => $_GET['value1'], 'key2' => $_GET['value2']);

foreach($array as $key => $value)
{
    if(empty($value))
    {
        echo "$key empty<br/>";
    }
    else
    {
        echo "$key not empty<br/>";
    }
}

?>

With:

http://jfcoder.com/test/arrayempty.php?value1=somevalue

EDIT: And this - http://jfcoder.com/test/arrayempty.php?value1=somevalue&value2=

I get:

key1 not empty
key2 empty

I'm thinking you need to give more context to your question about actual use, since there is probably some other detail throwing it off.

EDIT

This doesn't make any sense, but this was provided in a comment:

$array = array(
    'key1' => '".mysql_real_escape_string($_GET['value1'])."',
    'key2' => '".mysql_real_escape_string($_GET['value2'])."'
);

This actually doesn't parse due to the single quotes being mixed in with the single quoted string, and the fact that this, even if it did parse, would set the array piece equal to the literal string, not the function result.

I gather that maybe it was:

$array = array(
    'key1' => "'".mysql_real_escape_string($_GET['value1'])."'",
    'key2' => "'".mysql_real_escape_string($_GET['value2'])."'"
);

Which would indicate that a string would always be returned that was at least '', which would never be empty, due to the single quotes.

Consider PDO.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • Yes, i actually also do this $array = array('key1' => '".mysql_real_escape_string($_GET['value1'])."', 'key2' => '".mysql_real_escape_string($_GET['value2'])."'); – dikidera Mar 29 '11 at 22:56
  • @mcfe - You need to add it to your question. – Jared Farrish Mar 29 '11 at 22:58
  • @mcfe: Right, so actually, your question doesn't represent your situation at all. `mysql_real_escape_string` requires and uses a database connection; are you sure you meant this? And did you `print_r($array)` to check that its contents match with what you think they should be? – Lightness Races in Orbit Mar 29 '11 at 22:59
  • Jared Farrish actually did get it work, because the way i use mysql_real_ecape_string i also returns the single quotes..so that is why they were never empty and were always set. And yes, i am using MySQL that is why i used the mysql_real... function – dikidera Mar 29 '11 at 23:00
  • I got to thinking about this, and as far as I can tell, `mysql_real_escape_string()` does not return a single-quoted string. @mcfe, I posted your comment code above. Please see my remarks. Something isn't adding up. – Jared Farrish Mar 29 '11 at 23:25
  • Yup, since i found my problem i hurried with the comment, but yeah, i use a customized function of mysql_real_esc... which returns "'".mysql_real_escape_string($query)."'". – dikidera Mar 30 '11 at 02:27
0

OH THIS WILL DO IT! this will add the value key pair to a new array.

if (isset($_GET)) {
  $get_keys = array_keys($_GET);
  foreach ($array_keys as $k) {
    if ($_GET[$k] != '') {
      $new_array[$k] = $_GET[$k];
    }
  }
}
Chris McClellan
  • 1,123
  • 7
  • 14
-1

I would check to see if the global $_GET var is set then loop through the values.

EDIT: PUSH GET VARS ONTO ARRAY IF THEY ARE SET (this will push the values onto an array and assign it value1, value2, etc.

if (isset($_GET)) {
  foreach ($_GET as $get) {
    if ($get != '') {
      $new_array['value'.$i] = $get;
      ++$i;
    }
  }
}
Chris McClellan
  • 1,123
  • 7
  • 14
  • 1
    i quote from above `http://mysite.com/page.php?value1=somevalue meaning that value2 is missing` that means going thru the `$_GET` vars wont help him in the least bit.. – Naftali Mar 29 '11 at 22:52
  • According to the question, _value2 is empty or ...missing from the query string_, this won't work if it's missing. – Wrikken Mar 29 '11 at 22:52
-1

Your foreach() loop is scanning only given values, not expected values. You need to define expected values and do a loop like this:

foreach($expected_keys as $key) {
    if ( (!isset($array[$key])) || empty($array[$key]) ) {
        //spout some error
    }
}
Skrol29
  • 5,402
  • 1
  • 20
  • 25
-2

empty() also performs an isset().

So if the key is missing, empty() won't cause an error. It validate as TRUE because it is empty

you would need to do a seperate isset() check to make sure the key is there

Jase
  • 599
  • 3
  • 9
  • Would you give me an example, as i can't get into the logic of this – dikidera Mar 29 '11 at 22:49
  • You are setting the $_GET straight into the array. Perform checks like isset($_GET['key']) to make sure the key exists. – Jase Mar 29 '11 at 22:50
  • If the key is missing, empty() would return true – thus print an error msg.. But, remember: if the key actually is missing: that means the key/value-set is missing from the array. So "foreach" will not check the wanted key – qualbeen Mar 29 '11 at 22:54
  • read my comment above your own. I mention he needs to perform the isset() on the $_GET – Jase Mar 29 '11 at 22:54
  • @Jase: He doesn't "need" to at all. He could, but not doing so is not causing any problem here. He's doing `foreach` over `$array` (filled with _expected_ keys), not `$_GET` (filled with _provided_ keys). – Lightness Races in Orbit Mar 29 '11 at 22:59