XML Array to Mysql

Discussion in 'PHP' started by phpnewb, Mar 18, 2010.

  1. #1
    I have the following php code to fetch the xml file and spit it out as an array.



    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $result, $vals, $index);
    xml_parser_free($parser);

    $params = array();
    $level = array();

    foreach ($vals as $xml_elem) {
    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);
    }
    }

    echo "<pre>";
    print_r ($params);
    echo "</pre>";


    How do I take that array and dump each item into a mysql database.
    This is how the Array looks



    [CatalogInfo] => Array
    (
    [LastUpdated] => 2010-03-17 01:48:44
    [asdJs] => asd8710852673470090/js
    [CurrList] => USD
    )

    [Catalog] => Array
    (
    [AX0555OR] => Array
    (
    [Description] =>
    [Type] => 1
    [SubType] => 5
    [DateAdded] => 2010-03-17 01:44:14
    [DateChanged] => 2010-03-17 01:44:14
    [NumLayouts] => 3
    [ThumbWidth] => 150
    [ThumbHeight] => 160
    [Themes] => 1
    [Styles] => 3,8
    [Features] => 6
    [Resolution] => 3
    [asdAff] => AX0555OR
    [Price] => 24.99
    [Layouts] => Array
    (
    [1] => Array
    (
    [Title] => Homepage
    [asdThumbnail] => AX0555OR.gif
    [asdPreview] => AX0555OR.jpg
    )

    [2] => Array
    (
    [Title] => Subpage 1
    [asdThumbnail] => AX0555OR_2.gif
    [asdPreview] => AX0555OR_2.jpg
    )

    [3] => Array
    (
    [Title] => Subpage 2
    [asdThumbnail] => AX0555OR_3.gif
    [asdPreview] => AX0555OR_3.jpg
    )

    )

    [JsLinks] => Array
    (
    [asdCart] => am_cart_add('AX0555OR');
    [asdBuy] => am_buy('AX0555OR');
    [asdPreview] => ow_preview_full('AX0555OR');
    )

    )
     
    phpnewb, Mar 18, 2010 IP
  2. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #2
    you can use 2 method SERIALIZE or JSON.
     
    guardian999, Mar 18, 2010 IP
  3. ceo.ahlul

    ceo.ahlul Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you want to save as array you can use serialize as guardian999 said and when retrieve you can use unserialize to convert it into array again.

    But if you mean want to insert each item of array you can use foreach

    Sample:

    foreach($xmlarraysample as $item) {
    mysql_query("INSERT (.....) VALUES ('$item[0]','$item[1]......)'
    }
     
    ceo.ahlul, Mar 19, 2010 IP