On Mon, Feb 25, 2013 at 9:51 PM, Karl DeSaulniers <k...@designdrumm.com> wrote:
> Never mind. I found a different function that reads out the children as well
> into the array.
>
>
>                 function xml_parse_into_assoc($data) {
>                           $p = xml_parser_create();
>
>                           xml_parser_set_option($p, XML_OPTION_CASE_FOLDING,
> 0);
>                           xml_parser_set_option($p, XML_OPTION_SKIP_WHITE,
> 1);
>
>                           xml_parse_into_struct($p, $data, $vals, $index);
>                           xml_parser_free($p);
>
>                           $levels = array(null);
>
>                           foreach ($vals as $val) {
>                             if ($val['type'] == 'open' || $val['type'] ==
> 'complete') {
>                               if (!array_key_exists($val['level'], $levels))
> {
>                                 $levels[$val['level']] = array();
>                               }
>                             }
>
>                             $prevLevel =& $levels[$val['level'] - 1];
>                             $parent = $prevLevel[sizeof($prevLevel)-1];
>
>                             if ($val['type'] == 'open') {
>                               $val['children'] = array();
>                               array_push(&$levels[$val['level']], $val);
>                               continue;
>                             }
>
>                             else if ($val['type'] == 'complete') {
>                               $parent['children'][$val['tag']] =
> $val['value'];
>                             }
>
>                             else if ($val['type'] == 'close') {
>                               $pop = array_pop($levels[$val['level']]);
>                               $tag = $pop['tag'];
>
>                               if ($parent) {
>                                 if (!array_key_exists($tag,
> $parent['children'])) {
>                                   $parent['children'][$tag] =
> $pop['children'];
>                                 }
>                                 else if
> (is_array($parent['children'][$tag])) {
>                                     if(!isset($parent['children'][$tag][0]))
> {
>                                         $oldSingle =
> $parent['children'][$tag];
>                                         $parent['children'][$tag] = null;
>                                         $parent['children'][$tag][] =
> $oldSingle;
>
>                                     }
>                                       $parent['children'][$tag][] =
> $pop['children'];
>                                 }
>                               }
>                               else {
>                                 return(array($pop['tag'] =>
> $pop['children']));
>                               }
>                             }
>
>                             $prevLevel[sizeof($prevLevel)-1] = $parent;
>                           }
>                 }
>
>
>                         $params = xml_parse_into_assoc($result);//$result =
> xml result from USPS api
>
> Original function by: jemptymethod at gmail dot com
> Duplicate names fix by: Anonymous (comment right above original function)
>
> Best,
> Karl
>
>
>
> On Feb 25, 2013, at 7:50 PM, Jim Lucas wrote:
>
>> On 02/25/2013 05:40 PM, Karl DeSaulniers wrote:
>>>
>>> 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(
>>> specialservice => 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.
>>>
>>> TIA,
>>>
>>> Best,
>>>
>>> Karl DeSaulniers
>>> Design Drumm
>>> http://designdrumm.com
>>>
>>>
>>
>> This will never work.  Your last array will always overwrite your previous
>> array.
>>
>> Here is how I would suggest building it:
>>
>>
>> $items = array(
>>    1 => array(
>>        serviceid => 1,
>>        servicename=> signature required,
>>        price => $4.95
>>    ),
>>    15 => array(
>>        serviceid => 15,
>>        servicename => return receipt,
>>        price => $2.30
>>    )
>> )
>>
>> This will ensure that your first level indexes never overwrite themselves.
>>
>> But, with that change made, then do this:
>>
>> foreach ( $items AS $item ) {
>>  if ( array_key_exists('price', $item) ) {
>>    echo $item['price'];
>>  } else {
>>    echo 'Item does not have a price set';
>>  }
>> }
>>
>> Resources:
>> http://php.net/foreach
>> http://php.net/array_key_exists
>>
>> --
>> Jim Lucas
>>
>> http://www.cmsws.com/
>> http://www.cmsws.com/examples/
>
>
> Karl DeSaulniers
> Design Drumm
> http://designdrumm.com
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Would this work for you?
http://us.php.net/manual/en/function.xml-parse-into-struct.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to