开发者

How do I determine whether or not a particluar Key Combination is already in a MULTI-DIMENSIONAL associative array in PHP?

To simplify this posed question, assume that each cell has a Row Name and a Column name that properly maps you to the appropriate cell. I'm looping through DB records and creating a location for certain fields in a 2D array that I'll be returning to the caller. My question is how can I tell if a cell already 开发者_Go百科exists at array[rownName][colName]?

Here's a high level view of what I'm attempting to do:

  //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'];    
  }

Thanks in advance for all of your help SO!


You can use array_key_exists or isset if you just want to check that particular keys have already been set:

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'];
}

or:

if(isset($ret[$row['output_row_id']][$row['output_name']])) {

    $ret[$row['output_row_id']][$row['output_name']] = $row['output_value'];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜