Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

how to get values from a specific php array key WordPress

  • SOLVED

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!

Answers (7)

2012-01-27

Luis Abarca answers:

$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
}


Robert Seyfriedsberger comments:

that was the solution I was looking for - thanks!

2012-01-27

Kailey Lampert answers:

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

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

2012-01-27

Julio Potier answers:

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 ?

2012-01-27

John Cotton answers:

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' : '';


Robert Seyfriedsberger comments:

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

2012-01-27

Christianto answers:

Hi,

How about php [[LINK href="http://php.net/manual/en/function.array-key-exists.php"]]array_key_exists[[/LINK]].
Have you look at it?

2012-01-27

Mike Van Winkle answers:

You could also just check if each value is set:


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



Robert Seyfriedsberger comments:

the problem is that if I use the code

echo $wmslayer['lwms'];

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

2012-01-27

Francisco Javier Carazo Gil answers:

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:
<code>
array_values($array);
array_keys($array)