将数组值与字符串进行比较 [英] Compare array value with string

查看:104
本文介绍了将数组值与字符串进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含各种产品的数组

$ inventory = array();

$ inventory [0] =" whatever1";



我有一个包含各种值的数组

$ quantity = array(100,200,300);


我打电话给来自$ _POST [itemname]形式的值;说

我指定一个变量

$ var1 =" $ _ POST [itemname]" ;;


我要比较一下具有数组值的输入值例如

for($ i = 0; $ i< $ inventory_total; $ i ++){

if($ inventory [$ i] = = $ item){code}

}

但是,这总是负数,因为我认为我无法将数组项与字符串进行比较。

到目前为止我唯一发现的是mplode。声明

但我想比较单个数组项而不是由所有数组组成的字符串。

有人可以建议一个简单的转换,以便我可以比较值吗?

解决方案

inventory = array();


inventory [0] =" whatever1";



我有一个包含各种值的数组


quantity = array(100,200,300);


我从表单中调用一个值

I have an array containing the various products
$inventory = array();
$inventory[0] = "whatever1";
etc
I have an array containing various values
$quantity = array(100,200,300);

I call in a value from a form $_POST[itemname]; say
I assign a variable
$var1="$_POST[itemname]";

I want to compare the input value with the array value e.g.
for($i = 0;$i<$inventory_total;$i++) {
if($inventory[$i] == $item) { code }
}
However, this is always negative as I believe I cannot compare an array item with a string.
The only thing I''ve found so far is the "mplode" statement
But I want to compare individual array items not a string composed of all of them.
Can anyone suggest a simple conversion so that I can compare values?

解决方案

inventory = array();


inventory[0] = "whatever1";
etc
I have an array containing various values


quantity = array(100,200,300);

I call in a value from a form


这篇关于将数组值与字符串进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆