Reputation: 681
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;
}
}
break;
How I print the result.
array_filter($tempArray = array_map("unserialize", array_unique(array_map("serialize", $tempArray))));
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($tempArray as $tVars)
{
echo '<url>
<loc>
'.$tVars["loc"].'
</loc>
<lastmod>'.$tVars["vdate"].'</lastmod>
<changefreq>weekly</changefreq>
</url>';
}
echo '</urlset>';
I am getting undefined index error for $tempArray[]['vdate']
. Is there anything am I missing?
Upvotes: 1
Views: 367
Reputation: 527
You can set Key.
$i = 0;
foreach($dbResult as $pResult)
{
$tempArray[$i]['loc'] = convert_to_URL($pResult->id, $pResult->title);
$tempArray[$i]['vdate'] = $pResult->vdate;
$i++;
}
Upvotes: 0
Reputation: 28529
$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
<?php
$array=array();
$array[]['a'] = 'a';
$array[]['b'] = 'b';
echo json_encode($array);
result:[{"a":"a"},{"b":"b"}]
Upvotes: 1