Lunatic Fnatic Lunatic Fnatic - 5 months ago 38
PHP Question

PHP Array Undefined Index Issue

I am creating a sitemap using XML. When I try to put multiple values to array, I am getting undefined index for the second variable.

Sample code below.

How I gather the results:

$tempArray = array();
switch ($type)
case "v":
$dbResult = $db->get_dbResult("Select title,id,vdate from " . DB_PREFIX . "tpV order by id desc " . this_offset($getFirstK));
if ($dbResult)
foreach($dbResult as $pResult)
$tempArray[]['loc'] = convert_to_URL($pResult->id, $pResult->title);
$tempArray[]['vdate'] = $pResult->vdate;


How I print the result.

array_filter($tempArray = array_map("unserialize", array_unique(array_map("serialize", $tempArray))));
echo '<urlset xmlns="">';

foreach($tempArray as $tVars)
echo '<url>
echo '</urlset>';

I am getting undefined index error for
. Is there anything am I missing?

$tempArray[]['loc']   = convert_to_URL($pResult->id, $pResult->title);
$tempArray[]['vdate'] = $pResult->vdate;

here you create two arrays, one with loc key, another with vdate, so one of the two keys must be undefined in the two arrys.

to fix this error, just change the two lines to

$tempArray[] = array('loc' => convert_to_URL($pResult->id, $pResult->title), 'vdate' => $pResult->vdate);

here is a demo

  $array[]['a'] = 'a';
  $array[]['b'] = 'b';
  echo json_encode($array);