在PHP中,如何确定特定键组合是否已经存在于多维关联数组中?
为了简化这个问题,假设每个单元格都有一个行名和一个列名,可以正确地将您映射到相应的单元格。我正在循环遍历DB记录,并为2D数组中的某些字段创建一个位置,我将返回给调用者。我的问题是如何判断数组[rownName][colName]中是否已经存在单元格 下面是我尝试做的一个高级视图:在PHP中,如何确定特定键组合是否已经存在于多维关联数组中?,php,multidimensional-array,key,Php,Multidimensional Array,Key,为了简化这个问题,假设每个单元格都有一个行名和一个列名,可以正确地将您映射到相应的单元格。我正在循环遍历DB记录,并为2D数组中的某些字段创建一个位置,我将返回给调用者。我的问题是如何判断数组[rownName][colName]中是否已经存在单元格 下面是我尝试做的一个高级视图: //While there are more records: while ($row = mysql_fetch_assoc($result)) { //If this key doeesn'
//While there are more records:
while ($row = mysql_fetch_assoc($result)) {
//If this key doeesn't already exist in the return array,
//add this key/value pair.
//Proper logic for determining whether or not a cell has already been
//created for this record would go here...
$ret[$row['output_row_id']][$row['output_name']] = $row['output_value'];
}
提前感谢您的帮助 您可以使用或如果您只想检查特定的键是否已设置:
if(array_key_exists($row['output_row_id'],$ret)
&& array_key_exists($row['output_name'],$ret[$row['output_row_id']])) {
$ret[$row['output_row_id']][$row['output_name']] = $row['output_value'];
}
或:
这很有效。我在查找数组密钥的文档,它似乎不支持多维密钥。谢谢
if(isset($ret[$row['output_row_id']][$row['output_name']])) {
$ret[$row['output_row_id']][$row['output_name']] = $row['output_value'];
}