Re: Array - partical path to the array's path (part 2)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Got it figured out by now.  I noticed one small bug but I'll fix it monday
morning, so i'm going to post what I did to make this work..

I'm going to donate this code to the guy at
http://www.devdump.com/phpxml.php and hopefully he'll put some of this to a
good use as more and more people are now using xml to exchange data, so this
would be very helpful to people who may need this...

[code]
$array = array
(
  'NEWSFEED' => array
  (
    '0' => array
    (
      'ATTRIBUTES' => array
      (
        'ID' => 'test1'
      ),
      'MESSAGE' => array
      (
        '0' => array
        (
          'ATTRIBUTES' => array
          (
            'ID' => 'test2',
            'TID' => 'test4'
          ),
          'TITLE' => array
          (
            '0' => array
            (
               'VALUE' => 'Title #1'
            ),
            '1' => array
            (
               'VALUE' => 'Title #2'
            )
          ),
          'QUOTE' => array
          (
            '0' => array ( 'VALUE' => 'Quote #1' )
          ),
          'BODY' => array
          (
            '0' => array ( 'VALUE' => 'Body #1' )
          ),
          'REFERENCE' => array
          (
            '0' => array ( 'VALUE' => 'Reference #1' )
          )
        ),
        '1' => array
        (
          'TITLE' => array
          (
            '0' => array ( 'VALUE' => 'Title #2' )
          ),
          'BODY' => array
          (
            '0' => array ( 'VALUE' => 'Body #2' )
          ),
          'REFERENCE' => array
          (
            '0' => array ( 'VALUE' => '' )
          )
        ),
        '2' => array
        (
          'ATTRIBUTES' => array
          (
            'ID' => 'test3'
          ),
          'TITLE' => array
          (
            '0' => array ( 'VALUE' => 'Title #3' )
          ),
          'BODY' => array
          (
            '0' => array ( 'VALUE' => 'Body #3' )
          ),
          'IMAGE' => array
          (
            '0' => array ( 'VALUE' => 'Image #3' )
          ),
          'REFERENCE' => array
          (
            '0' => array ( 'VALUE' => 'Reference #3' )
          )
        )
      )
    )
  ),
  'BLOG' => array
  (
    '0' => array
    (
      'MESSAGE' => array
      (
         '0' => array ( 'VALUE' => 'Title #1' )
      )
    )
  )
);

  function XmlTreeImplode($tree, $branch_level, &$array_key, &$xml_string)
  {
     $branch_level++;

     $tab_spacer = " ";  //Options to use white spaces or html spaces,
etc...

     foreach($tree as $key => $value)
     {
        if (strtoupper($key) != "ATTRIBUTES") {
           $array_key[count($array_key)] = $key;
        }

        if ($branch_level % 2 != 0) {  //xml tag's name...
           if (strtoupper($key) != "ATTRIBUTES") {
              if(!(is_array($value))) { //Check for VALUE or CDATA name
tag...
                 $xml_string .= str_repeat($tab_spacer,
($branch_level-1))."<".$key.">".$value."</".$key.">"."\r";
              } else {  //Continue...
                 //=================================
                 //Grab the attribute data if any...
                 //=================================
                 if (is_array($value['0']['ATTRIBUTES'])) {
                    foreach($value['0']['ATTRIBUTES'] as $key1 => $value1) {
                       $attribute .= " ".$key1."=\"".$value1."\"";
                    }
                 }
                 //=================================

                 //Otherwise
                 $xml_string .= str_repeat($tab_spacer,
($branch_level-1))."<".$key.$attribute.">"."\r";
                 XmlTreeImplode($value, $branch_level, $array_key,
$xml_string);
                 $xml_string .= str_repeat($tab_spacer,
($branch_level-1))."</".$key.">"."\r";
              }
           }
        } else {  //xml name-tag counter...
           if ($key != 0) {
              //=================================
              //Grab the attribute data if any...
              //=================================
              if (is_array($value['ATTRIBUTES'])) {
                 foreach($value['ATTRIBUTES'] as $key1 => $value1) {
                    $attribute .= " ".$key1."=\"".$value1."\"";
                 }
              }
              //=================================

              $xml_string .= str_repeat($tab_spacer,
($branch_level-1))."</".$array_key[$branch_level-2].">"."\r";
              $xml_string .= str_repeat($tab_spacer,
($branch_level-1))."<".$array_key[$branch_level-2].$attribute.">"."\r";
           }

           XmlTreeImplode($value, $branch_level, $array_key, $xml_string);
        }
     }
  }

  $xml_string = GetXmlStr($array);
[/code]

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux