简体   繁体   English

检查是否在数组中然后在数组中的更好方法

[英]better way to check if in array and then in array

I am adding the following code to check whether a checkbox should be checked or not: 我正在添加以下代码来检查是否应选中一个复选框:

is_array($current_color_id_array) ? in_array('1', $current_color_id_array) : ''

I'm just wondering if there is nicer/more concise way to do it perhaps? 我只是想知道是否可能有更好/更简洁的方法?

Edit: Sorry I was not clear enough, and actually my code was wrong.. 编辑:对不起,我还不够清楚,实际上我的代码是错误的。

This is supposed to check if $current_color_id_array is an array because apparently if I don't check it will give me the error Warning: in_array() expects parameter 2 to be array 这应该检查$ current_color_id_array是否为数组,因为显然如果我不检查它会给我错误警告:in_array()期望参数2为数组

and it should also check if the value is in the array to mark the checkbox as checked. 并且还应该检查该值是否在数组中,以将复选框标记为选中状态。

this is all part of a checkbox function: 这是复选框功能的全部:

function tep_draw_checkbox_field($name, $value = '', $checked = false, $compare = '') {
    return tep_draw_selection_field($name, 'checkbox', $value, $checked, $compare);
  }

it is the third parameter. 它是第三个参数。 So if I can reask this I would say is there a better way of writing: 因此,如果我能对此有所收获,我会说有一种更好的书写方式:

tep_draw_checkbox_field('color_id[]', '1', (is_array($current_color_id_array) && in_array('1', $current_color_id_array)))

and just in case, this is how the array is made: 以防万一,这是数组的制作方式:

$color_id_query = tep_db_query("select color_id from " . TABLE_PRODUCTS_TO_COLORS . " where products_id = '" . (int)$product['products_id'] . "'");

    while ($color_id_row = mysql_fetch_array ($color_id_query)) {
    $current_color_id_array[] = $color_id_row['color_id'];
    }

The condition you have above could be more concisely written as: 您上面的条件可以更简洁地写为:

is_array($current_color_id_array) && in_array('1', $current_color_id_array)

This is actually slightly different than what you had: your code will evaluate to either true or the empty string, this will be either true or false . 这实际上与您所拥有的稍有不同:代码将评估为true或空字符串,这将为truefalse

Of course, you could shrink the condition even more if you weren't checking if $current_color_id_array is an array. 当然,如果不检查$current_color_id_array是否为数组,则可以进一步缩小条件。 The variable's got "array" right there in the name, does your code actually leave open a possibility that it's not really an array? 公司得到了在名为“阵列”那里的变量,并您的代码实际上留下打开的可能性,这是不是一个真正的数组?

Edit: Looking at your expanded question, since you're completely in charge of creating the array, it's easy to avoid the need to "make sure" your array exists. 编辑:考虑到您提出的问题,由于您完全负责创建数组,因此很容易避免需要“确保”数组存在。 Just make sure you're initializing your array variable, and you can be sure it will always be set and that it will always be an array. 只要确保您正在初始化数组变量,就可以确保将其始终设置并且将始终是一个数组。

$current_color_id_array = array();

$color_id_query = tep_db_query("select color_id from " . TABLE_PRODUCTS_TO_COLORS . " where products_id = '" . (int)$product['products_id'] . "'");
while ($color_id_row = mysql_fetch_array ($color_id_query)) {
    $current_color_id_array[] = $color_id_row['color_id'];
}

Then, you can just directly check for 1 in your array, and there's no chance you'll get a warning: 然后,您可以直接在数组中检查1 ,并且没有机会收到警告:

in_array('1', $current_color_id_array)

In light of the comment to create a function to echo the text, you could use this function : 根据注释创建一个回显文本的功能,您可以使用以下功能

function is_array_value($a,$b){
return (is_array($b)&&in_array($a, $b)) ? true : false;
}

The only thing I could think of would be to guarantee that $current_color_id_array is actually an array, by including 我唯一能想到的就是通过包含以下内容来保证$current_color_id_array 实际上是一个数组

$current_color_id_array = array();

somewhere at the beginning of your script. 脚本开始处的某个位置。

I would probably combine them into one conditional statement like so: 我可能会将它们组合成一条条件语句,如下所示:

$someBool = (is_array($myArray) && in_array('1', $myArray, true));

Note that you should use strict comparison with the in_array() function when checking things like '1'. 请注意,在检查“ 1”之类的内容时,应与in_array()函数进行严格比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM