views:

38

answers:

1

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 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!

+2  A: 

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'];
}
karim79
This worked perfectly. I was looking the documentation for array_key_exists and it didn't seem as if it supported multidimensional keys. Thanks!
WillMatt