Re: Arrays
| From: | Karl DeSaulniers | Date: | Tue, 26 Feb 2013 02:19:10 +0000 |
| Subject: | Re: Arrays | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320297@lists.php.net to get a copy of this message | ||
On Feb 25, 2013, at 7:48 PM, Adam Richardson wrote:
On Mon, Feb 25, 2013 at 8:40 PM, Karl DeSaulniers <karl@designdrumm.comwrote:Hi Adam, Actually you are correct. Sorry to confuse. Its an XML response from the USPS api I am going through, which I am converting to an array. Here is the response that I am trying to convert to a multidimensional array. <?xml version="1.0"?> <RateV4Response><Package ID="1ST"><ZipOrigination>75287</ZipOrigination><ZipDestination>87109</ZipDestination><Pounds>70</Pounds><Ounces>0</Ounces><Container>RECTANGULAR</Container><Size>LARGE</Size><Width>2</Width><Length>15</Length><Height>10</Height><Zone>4</Zone><Postage CLASSID="1"><MailService>Priority Mail<sup>&reg;</sup></MailService><Rate>62.95</Rate><SpecialServices><SpecialService><ServiceID>1</ServiceID><ServiceName>Insurance</ServiceName><Available>true</Available><AvailableOnline>true</AvailableOnline><Price>1.95</Price><PriceOnline>1.95</PriceOnline><DeclaredValueRequired>true</DeclaredValueRequired><DueSenderRequired>false</DueSenderRequired></SpecialService><SpecialService><ServiceID>0</ServiceID><ServiceName>Certified Mail<sup>&reg;</sup></ServiceName><Available>true</Available><AvailableOnline>false</AvailableOnline><Price>3.10</Price><PriceOnline>0</PriceOnline></SpecialService><SpecialService><ServiceID>19</ServiceID><ServiceName>Adult Signature Required</ServiceName><Available>false</Available><AvailableOnline>true</AvailableOnline><Price>0</Price><PriceOnline>4.95</PriceOnline></SpecialService></SpecialServices></Postage></Package><Package ID="2ND"><ZipOrigination>75287</ZipOrigination><ZipDestination>87109</ZipDestination><Pounds>55</Pounds><Ounces>0</Ounces><Container>RECTANGULAR</Container><Size>LARGE</Size><Width>2</Width><Length>15</Length><Height>10</Height><Zone>4</Zone><Postage CLASSID="1"><MailService>Priority Mail<sup>&reg;</sup></MailService><Rate>52.55</Rate><SpecialServices><SpecialService><ServiceID>1</ServiceID><ServiceName>Insurance</ServiceName><Available>true</Available><AvailableOnline>true</AvailableOnline><Price>1.95</Price><PriceOnline>1.95</PriceOnline><DeclaredValueRequired>true</DeclaredValueRequired><DueSenderRequired>false</DueSenderRequired></SpecialService><SpecialService><ServiceID>0</ServiceID><ServiceName>Certified Mail<sup>&reg;</sup></ServiceName><Available>true</Available><AvailableOnline>false</AvailableOnline><Price>3.10</Price><PriceOnline>0</PriceOnline></SpecialService><SpecialService><ServiceID>19</ServiceID><ServiceName>Adult Signature Required</ServiceName><Available>false</Available><AvailableOnline>true</AvailableOnline><Price>0</Price><PriceOnline>4.95</PriceOnline></SpecialService></SpecialServices></Postage></Package></RateV4Response> This is my attempt to convert. I thought of setting up a blank array then filling that array with the specialservice arrays $data = strstr($result, '<?'); // echo '<!-- '. $data. ' -->'; // Uncomment to show XML in comments $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser,XML_OPTION_TARGET_ENCODING, "ISO-8859-1"); xml_parse_into_struct($xml_parser, $result, $vals, $index); xml_parser_free($xml_parser); $params = array(); $level = array(); foreach ($vals as $xml_elem) {Hi Guys/Gals, If I have an multidimensional array and it has items that have the same name in it, how do I get the values of each similar item? EG: specialservices => array(Something appears to be amiss, as your array couldn't contain multiple items with the specialservice key (I'm assuming the second key 'secialservice' is just a typo), as any subsequent assignments would overwrite the previous value. Adam --Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.comspecialservice => array( serviceid => 1, servicename=> signature required, price => $4.95 ), secialservice => array( serviceid => 15, servicename => return receipt, price => $2.30 )) How do I get the prices for each? What would be the best way to do this? Can I utilize the serviceid to do this somehow? It is always going to be different per specialservice.
if ($xml_elem['type'] == 'open') {
if (array_key_exists('attributes',$xml_elem)) {
list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
} else {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
}
if ($xml_elem['type'] == 'complete') {
$start_level = 1;
$php_stmt = '$params';
while($start_level < $xml_elem['level']) {
$php_stmt .= '[$level['.$start_level.']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
eval($php_stmt);
}
}
... then trying to pull data from the results of the $php_stmt
$sid = array();
$numserv = count($params['RATEV4RESPONSE'][''.$p.$ack.''][$cId]['SPECIALSERVICES']);
if($numserv > 0) {
foreach($params['RATEV4RESPONSE'][''.$p.$ack.''][$cId]['SPECIALSERVICES']['SPECIALSERVICE'] as $sp_servs) {
$sid_val = $sp_servs['SERVICEID'];
if(in_array($sid_val, $sid)) {
return;
} else {
if($sp_servs['AVAILABLEONLINE'] == true) {
$extracost += $sp_servs['PRICEONLINE'];
} else {
$extracost += $sp_servs['PRICE'];
}
$sid[$i] = $sid_val;
}
}
}
echo("<br><br>Services Array :<br>");
$num_array_items = count($sid);
for($i=0; $i < $num_array_items; $i++) {
echo($i.". ".$sid[$i]."<br>");
}
Karl DeSaulniers
Design Drumm
http://designdrumm.com