|
-
Aug 3rd, 2006, 05:07 AM
#1
Thread Starter
Hyperactive Member
Accessing data within array
OK,
I have an array full of data - how on earth do I access that data, if I print_r($data) it echos it onto the screen ok I just cant get it to output what I want. I have tried accessing it thro $data['var'], $data[0] etc but I cant get it to produce.
below is the code that I am using.
PHP Code:
<?php
function GetXMLTree ($xmldata)
{
// we want to know if an error occurs
ini_set ('track_errors', '1');
$xmlreaderror = false;
$parser = xml_parser_create ('ISO-8859-1');
xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
$xmlreaderror = true;
echo "error";
}
xml_parser_free ($parser);
if (!$xmlreaderror) {
$result = array ();
$i = 0;
if (isset ($vals [$i]['attributes']))
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
$result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, 'open'));
}
ini_set ('track_errors', '0');
return $result;
}
function GetChildren ($vals, &$i, $type)
{
if ($type == 'complete') {
if (isset ($vals [$i]['value']))
return ($vals [$i]['value']);
else
return '';
}
$children = array (); // Contains node data
/* Loop through children */
while ($vals [++$i]['type'] != 'close') {
$type = $vals [$i]['type'];
// first check if we already have one and need to create an array
if (isset ($children [$vals [$i]['tag']])) {
if (is_array ($children [$vals [$i]['tag']])) {
$temp = array_keys ($children [$vals [$i]['tag']]);
// there is one of these things already and it is itself an array
if (is_string ($temp [0])) {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
} else {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
$children [$vals [$i]['tag']][] = GetChildren ($vals, $i, $type);
} else
$children [$vals [$i]['tag']] = GetChildren ($vals, $i, $type);
// I don't think I need attributes but this is how I would do them:
if (isset ($vals [$i]['attributes'])) {
$attributes = array ();
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
// now check: do we already have an array or a value?
if (isset ($children [$vals [$i]['tag']])) {
// case where there is an attribute but no value, a complete with an attribute in other words
if ($children [$vals [$i]['tag']] == '') {
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']] = $attributes;
}
// case where there is an array of identical items with attributes
elseif (is_array ($children [$vals [$i]['tag']])) {
$index = count ($children [$vals [$i]['tag']]) - 1;
// probably also have to check here whether the individual item is also an array or not or what... all a bit messy
if ($children [$vals [$i]['tag']][$index] == '') {
unset ($children [$vals [$i]['tag']][$index]);
$children [$vals [$i]['tag']][$index] = $attributes;
}
$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
} else {
$value = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']]['value'] = $value;
$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
}
} else
$children [$vals [$i]['tag']] = $attributes;
}
}
return $children;
}
$url = "http://www.affiliate.viator.com/xml/affiliateXMLData.jsp?IATA=POS"; //URL of the XML FEED
$contents = file_get_contents($url);
$data = GetXMLTree ($contents);
print_r($data);
//echo $data['ProductPageURL'][1];
?>
-
Aug 4th, 2006, 04:33 AM
#2
Re: Accessing data within array
What does print_r($data) output?
-
Aug 4th, 2006, 02:23 PM
#3
Thread Starter
Hyperactive Member
Re: Accessing data within array
 Originally Posted by penagate
What does print_r($data) output?
hi,
print_r() produces ...
Code:
Array ( [ViatorAffiliateXMLData] => Array ( [xmlns:xsi] => http://www.w3.org/2001/XMLSchema-instance [xsi:noNamespaceSchemaLocation] => affiliate.xsd [CreationDate] => 2006-08-02T03:00:04+10:00 [SchemaVersion] => 1.0 [Currency] => USD [IATA] => Array ( [ProductItem] => Array ( [0] => Array ( [ProductType] => Sightseeing [ProductCode] => 4130POS_AW [ReleaseDate] => 20030101 [PulldownDate] => 20061229 [ProductPageURL] => http://www.affiliate.viator.com/brochure/product_show.jsp?CODE=4130POS_AW&ID=1010&PRODUCTID=1016&AUID= [ProductItemDetail] => Array ( [PriceDesciption] => Starting from USD $65.00 per person [ItemTitle] => Asa Wright Nature Center [ProductText] => Trinidad boasts of an ecological diversity unmatched in the Caribbean and the east coast features protected wetlands and miles of fine brown sand. On this five and a half hour tour, travel to the fascinating Asa Wright Nature Center, where you will experience rich bird life such as the Ruby-topaz humming-bird and the tufted coquette. [Duration] => 5.5 hours [Special] => 0 [Ranking] => 0 [Introduction] =>
Your journey begins with a drive through the eastern towns of Curepe, Tunapuna, Arouca and Arima. You will soon notice that the commerce in these towns generate a beehive of activities.
On reaching the Borough of Arima, enjoy the journey through the Blanchisseuse North Coast Road onto the fascinating Asa Wright Nature Center where you will stop for a picnic and to have a picturesque photo session viewing the different types of Birds, Butterflies, Flora and Fauna.
The ASA Wright Nature Center is a study and conservation center and retains some of its plantation history by the coffee, cocoa and citrus still found there. It has the only easily accessible colony of oil-birds and rich bird life includes the Ruby-topaz humming-bird and the tufted coquette.
Continuing through the Rainforest, where you will delight in the sights of small rural villages such as Chopara Bay, Las Cuevas Bay and Curepe Point.
[Continent] => Caribbean [Country] => Trinidad and Tobago [StateProvince] => Trinidad [City] => Port of Spain [Commences] => Port of Spain, Trinidad [URLList] => Array ( [URL] => Array ( [0] => Array ( [URLType] => IMAGE [ImageDescription] => Asa Wright Nature Center [URLLink] => http://www.affiliate.viator.com/graphicslib/4130/SITours/asa.jpg [ImageArtist] => [ImageSupplier] => ) [1] => Array ( [URLType] => THUMBNAIL [URLLink] => http://www.affiliate.viator.com/graphicslib/thumbs/4130/SITours/asa.jpg ) [2] => Array ( [URLType] => IMAGE [ImageDescription] => Asa Wright Nature Center [URLLink] => http://www.viator.com/graphicslib/4130/SITours/asa_wright.jpg [ImageArtist] => [ImageSupplier] => ) ) ) ) ) [1] => Array ( [ProductType] => Sightseeing [ProductCode] => 4130POS_CS [ReleaseDate] => 20030101 [PulldownDate] => 20061229 [ProductPageURL] => http://www.affiliate.viator.com/brochure/product_show.jsp?CODE=4130POS_CS&ID=1010&PRODUCTID=1016&AUID= [ProductItemDetail] => Array ( [PriceDesciption] => Starting from USD $40.00 per person [ItemTitle] => Saddle Drive Maracas Beach [ProductText] => The most popular beach in northern Trinidad is Maracas Beach and this is your main destination on this sightseeing tour. After a quick exploration of the city of Port of Spain, you'll journey via the Saddle Road to this beautiful getaway. Magnificent views await you on this 3 1/2 hour journey. [Duration] => 3 1/2 hours [Special] => 0 [Ranking] => 1 [Introduction] =>
Tropical splendor at its best! This tour commences with a drive around the city of Port of Spain and highlights all the main points of interest including Government House, The Queens Park Savannah, Botanical Gardens, Emperor Valley Zoo, National Museum, Churches, shopping and modern residential areas.
Your journey continues over the Saddle Road with a breathtaking view of the Moka Golf Course, on the way to the beautiful, sandy Maracas beach. The north coast's scenic beaches provide the perfect atmosphere for seclusion, rest and recreation. Dominated by densely forested peaks, the northern interior offers amazing mountain ranges, while the agricultural plains of central Trinidad provide a fascinating contrast.
Passing through the scenic mountains of the northern range, you then journey across the peaceful Santa Cruz Valley and onto the Lady Young Road. At this point, enjoy an amazing panoramic view of the whole city of Port of Spain!
[Continent] => Caribbean [Country] => Trinidad and Tobago [StateProvince] => Trinidad [City] => Port of Spain [Commences] => Port of Spain, Trinidad [URLList] => Array ( [URL] => Array ( [0] => Array ( [URLType] => IMAGE [ImageDescription] => Maracas Beach [URLLink] => http://www.affiliate.viator.com/graphicslib/4130/SITours/maracasbeach.jpg [ImageArtist] => [ImageSupplier] => ) [1] => Array ( [URLType] => THUMBNAIL [URLLink] => http://www.affiliate.viator.com/graphicslib/thumbs/4130/SITours/maracasbeach.jpg ) [2] => Array ( [URLType] => IMAGE [ImageDescription] => Saddle Road [URLLink] => http://www.viator.com/graphicslib/4130/SITours/saddle1.jpg [ImageArtist] => [ImageSupplier] => ) ) ) ) ) [2] => Array ( [ProductType] => Day Tour [ProductCode] => 4130POS_IC [ReleaseDate] => 20030101 [PulldownDate] => 20061229 [ProductPageURL] => http://www.affiliate.viator.com/brochure/product_show.jsp?CODE=4130POS_IC&ID=1010&PRODUCTID=1016&AUID= [ProductItemDetail] => Array ( [PriceDesciption] => Starting from USD $75.00 per person [ItemTitle] => Island Circle Tour [ProductText] => Heading south out of Port of Spain and down the west coast, your circle island journey begins. You'll see the spectacular scenery of places such as the Gulf of Paris, Mayaro Beach, and Manzanilla. You will also visit the town of San Fernando, the second largest city of Trinidad. [Duration] => 7 hours [Special] => 0 [Ranking] => 2 [Introduction] =>
This tour starts in the north of Trinidad and proceeds southwards along the West coast, alluring you to the spectacular scenery of the Gulf of Paris.
Continuing via the Central Plains and Point-a-Pierre, you will visit San Fernando, the second largest city of Trinidad and the commercial hub of the Southland.
The journey then takes you eastward, into areas overlooking the lush coconut palms which fringe the 14 mile Mayaro Beach. On the return, travel through Manzanilla, Cocoa Plantations and Virgin Forest all on the East Coast of the island.
Along the East-West corridor your journey will conclude with some magnificent views of the rural villages below.
[Continent] => Caribbean [Country] => Trinidad and Tobago [StateProvince] => Trinidad [City] => Port of Spain [Commences] => Port of Spain, Trinidad [URLList] => Array ( [URL] => Array ( [0] => Array ( [URLType] => IMAGE [ImageDescription] => Island Circle Tour [URLLink] => http://www.affiliate.viator.com/graphicslib/4130/SITours/bayshot.jpg [ImageArtist] => [ImageSupplier] => ) [1] => Array ( [URLType] => THUMBNAIL [URLLink] => http://www.affiliate.viator.com/graphicslib/thumbs/4130/SITours/bayshot.jpg ) [2] => Array ( [URLType] => IMAGE [ImageDescription] => Island Circle Tour [URLLink] => http://www.viator.com/graphicslib/4130/SITours/islandcircle.jpg [ImageArtist] => [ImageSupplier] => ) ) ) ) ) [3] => Array ( [ProductType] => Sightseeing [ProductCode] => 4130POS_CT [ReleaseDate] => 20030101 [PulldownDate] => 20061229 [ProductPageURL] => http://www.affiliate.viator.com/brochure/product_show.jsp?CODE=4130POS_CT&ID=1010&PRODUCTID=1016&AUID= [ProductItemDetail] => Array ( [PriceDesciption] => Starting from USD $38.00 per person [ItemTitle] => Port of Spain City Tour [ProductText] => The bustling capital of Trinidad, Port of Spain, is an exciting city that intricately blends ancient and modern worlds and has a wide selection of shopping, art galleries and restaurants. Famous landmarks, majestic buildings and lush vegetation await your discovery. [Duration] => 2 .5 hours [Special] => 0 [Ranking] => 3 [Introduction] =>
This tour is ideally suited for visitors coming to Trinidad and Tobago that provides a great introduction to the city of Port of Spain, the bustling capital, filled with an enchanting myriad of art galleries and restaurants.
Begin your tour on the Wrightson road waterfront, through to the old and new Port of Spain and explore roads of historical value like Abercromby Street where Red House is situated. This famous landmark is also known as the House of Parliament, built in 1906.
You will then visit sights such as the Hall of Justice, Trinity Cathedral, Woodford Square and the Queen's Park Savannah which is a 230 acre Park surrounded by beautiful trees and old gingerbread mansions. Some of these mansions include the Stollmeyer Castle which was built in 1904, and was reminiscent of the German Romantic style, the Queen's Royal College which now serves as a secondary school for over 1200 students, the "White Hall" a design inspired by the Venetian-style Palazzi and the Archbishop Palace conceived by an Irishman with an affection for Indian Architecture
Thanks Chris
-
Aug 5th, 2006, 02:42 AM
#4
Fanatic Member
Re: Accessing data within array
well i think what you want to do is access an array right?
so in the process of looping through the array one or more element of the array is also an array on its own,
say you have an array named myArray() with 5 elements and you want to loop through them.
but in the process one or more elements of myArray() is also an array,
say maybe item 2 and 4 is also an array,
if my illustrations are right then maybe you should try to check if an array element is also an array(if it is then parse it as you wish in script) and so on.
-
Aug 5th, 2006, 04:24 AM
#5
Re: Accessing data within array
Use this at the top of your code whenever you use print_r().
PHP Code:
header('Content-type: text/plain');
Then you will actually be able to see meaningful output from print_r(). It's simply a whole lot of nested arrays, you should be able to work out how to access it when you see the output formatted properly. 
Example:
Code:
Array (
[ViatorAffiliateXMLData] => Array (
[xmlns:xsi] => http://www.w3.org/2001/XMLSchema-instance
[xsi:noNamespaceSchemaLocation] => affiliate.xsd [CreationDate] => 2006-08-02T03:00:04+10:00
[SchemaVersion] => 1.0
[Currency] => USD
[IATA] => Array (
[ProductItem] => Array (
[0] => Array (
[ProductType] => Sightseeing
[ProductCode] => 4130POS_AW
[ReleaseDate] => 20030101
...
PHP Code:
$data['ViatorAffiliateXMLData']['IATA']['ProductItem'][0]['ProductCode'] == '4130POS_AW'
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|