logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
how to get values from a specific php array key

I am the developer of the mapsmarker.com WordPress mapping plugin and need help with an sql statement/php arrays.

my layer table has 10 columns (wms, wms2...wms10, boolean type, thus value 0 or 1) for storing the info if a wms layer is active or not.
For KML output of my maps, I now want to check, if each wms layer is set for a layer or not. My first thought was to create 10 sql statements:

  $layer = mysql_real_escape_string($_GET['layer']);
  $layer_wms_active = $wpdb->get_var('SELECT l.id, l.wms FROM '.$table_name_layers.' AS l WHERE l.id='.$layer);
  $layer_wms2_active = $wpdb->get_var('SELECT l.id, l.wms2 FROM '.$table_name_layers.' AS l WHERE l.id='.$layer);
  $layer_wms3_active = $wpdb->get_var('SELECT l.id, l.wms3 FROM '.$table_name_layers.' AS l WHERE l.id='.$layer);
...
  if ($layer_wms_active == '1') { echo 'output 1'; }
  if ($layer_wms2_active == '1') { echo 'output 1'; }
  if ($layer_wms3_active == '1') { echo 'output 1'; }
....

Problem: this is not very efficient, so I tried the following statment:

  $sql_wmslayer = 'SELECT l.id as lid, l.wms as lwms, l.wms2 as lwms2, l.wms3 as lwms3, l.wms4 as lwms4, l.wms5 as lwms5, l.wms6 as lwms6, l.wms7 as lwms7, l.wms8 as lwms8, l.wms9 as lwms9, l.wms10 as lwms10 FROM '.$table_name_layers.' AS l WHERE l.id='.$layer;
  $wmslayer = $wpdb->get_results($sql_wmslayer, ARRAY_A);

The problem now is, that I dont know how to search for a certain key in this array and only check if the corresponding value is 0 or 1. I looked up the php functions array_keys, array_value and array_search but didnt find out how to do so.

I tried the following, but all attempts didnt work:

if ($wmslayer['lwms'] == '1') { echo 'output 1'; }
if ($wmslayer['wms'] == '1') { echo 'output 1'; }
if ($wmslayer[wms] == '1') { echo 'output 1'; }
if ($wmslayer[lwms] == '1') { echo 'output 1'; }
if ($wmslayer[0] == '1') { echo 'output 1'; }
if ($wmslayer[1] == '1') { echo 'output 1'; }
if ($wmslayer['1'] == '1') { echo 'output 1'; }
if ($wmslayer['0'] == '1') { echo 'output 1'; }

Any help is appreciated!
Thanks!

This question has been answered.

Robert Harm | 01/27/12 at 12:04pm Edit


(7) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/27/12
    12:11pm
    Kailey Lampert says:

    If you print $wmslayer it should tell you exactly what keys are available in the object/array.

    echo '<pre>';
    print_r( $wmslayer );
    echo '</pre>';

  • avatar
    Last edited:
    01/27/12
    12:12pm
    Julio Potier says:

    Hello Robert

    did you try "in_array()" ?

    like "if (in_array( '1', $wmslayer ) ) echo 'output 1';"
    but i'm not sure that you just need that 1 value is set to 1.

    How many "output 1" have to be printed ?

  • avatar
    Last edited:
    01/27/12
    12:15pm
    John Cotton says:

    I don't quite understand the question (so why am I answering!!) but this will output 1 if the array has the specified key:


    echo in_array( 'lwms', array_keys($wmslayer)) ? 'output 1' : '';

    • 01/27/12 5:48pm

      Robert Harm says:

      well, actually I want to check if the key lwms is 0 or 1 and only output a variable, if it is 1

  • avatar
    Last edited:
    01/27/12
    12:17pm
    Christianto says:

    Hi,

    How about php array_key_exists.
    Have you look at it?

  • avatar
    Last edited:
    01/27/12
    12:38pm
    Mike Van Winkle says:

    You could also just check if each value is set:


    if(isset($wmslayer['lwms']) AND $wmslayer['lwms'] == 1)) {
    //do stuff
    }


    Previous versions of this answer: 01/27/12 at 12:38pm

    • 01/27/12 5:51pm

      Robert Harm says:

      the problem is that if I use the code

      echo $wmslayer['lwms'];

      I get the error Notice: Undefined index: lwms in ....

  • avatar
    Last edited:
    01/27/12
    1:20pm
    Francisco Javier Carazo Gil says:

    If you are getting results in an array asociative, before continuing, you can do a print_r to see the structure and content of array:

    print_r($wmslayer);


    You will obtain a string printed in screen with all data and structure.

    Start checking if there are any row:
    count($wmslayer);</count>

    And later, you can see all the values or all the keys in this way:

    array_values($array);
    array_keys($array)

  • avatar
    Last edited:
    01/27/12
    1:45pm
    Luis Abarca says:

    $wpdb->get_results() return an array, so you can try


    if ($wmslayer[0]['lwms'] == '1') { echo 'output 1'; }
    ...


    Or

    foreach ($wmslayer as $layer) {
    if ($layer['lwms'] == '1') { echo 'output 1'; }
    // ... other if's
    }

    Previous versions of this answer: 01/27/12 at 1:45pm

    • 01/27/12 5:56pm

      Robert Harm says:

      that was the solution I was looking for - thanks!

This question has expired.



Gabriel Reguly, idt, Christianto, Julio Potier, Kannan C, Robert Harm voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.