multidimensional array error - unidentified index

77 views Asked by At

As far as I know I have defined the variable, however I am getting the following error :

Notice : Undefined index: headerid in ..

function group_items($db_results) {
  $data = array();
  while($row = mysqli_fetch_assoc($db_results)) {
    $title_id = htmlentities($row['TitleID']);
    $title = htmlentities($row['MainTitle']);

    // group Main Title by title id
    $data[$title_id]['title'] = htmlentities($row['MainTitle']);

    // group data by title id
    $data[$title_id]['data'][] = array( 
      'headerid' => $row['headerid'],
      'title'     => htmlentities($row['title']),
      'content'   => htmlentities($row['content'])
      );
  }

// testing
if (isset($data['headerid'])) {
        echo "Yes";
    } else {
         echo "NO";
     }

      return $data;
    }

When I tested using both isset() and array_key_exists for true / false it resulted in false.

When using print_r I get the following: Array ( [1] => Array ( [title] => Promising practices regarding general principles of community policing [data] => Array ( [0] => Array ( [a] => 1 [title] => Initiating and developing coordination and collaboration actions between law enforcement agen [content] => Good cooperation and coordination relations between .... more content here

1

There are 1 answers

10
Mohamed Nakhlawy On

$data['headerid'] is not defined in you code.

Try Print_r($data); to check array elements.

At testing you need to edit your code to

isset($data[$title_id]['data'][0]['headerid']))