How to determine if a combination of keywords in a MULTI-DIMENSIONAL is an associative array in PHP?

To simplify this asked question, let's assume each cell has a row name and a column name that correctly maps you to the corresponding cell. I iterate over DB records and create locations for specific fields in a 2D array that I will return to the caller. My question is, how can I tell if a cell already exists in the array [role_name)] [colName]?

Here's a high level of what I'm trying 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 your help SO!

+2


source to share


1 answer


You can use array_key_exists

or isset

, if you just want to check that certain keys are already 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'];
}

      

+2


source







All Articles