如何使用php在数组中存储_attr键值?

时间:2011-07-08 07:02:09

标签: php xml

我使用xml2array.php将我的xml解析为数组。 它给出了像

这样的数组
Array(
 [HotelImage] => Array
 (
      [0] => Array
      (
      )

      [1] => Array
      (
      )

      [0_attr] => Array
      (
           [Type] => Bar/Lounge
           [URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-11.jpg
      )

      [1_attr] => Array
      (
           [Type] => Lobby
           [URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-8.jpg
      )

      [2] => Array
      (
      )

      [2_attr] => Array
      (
           [Type] => Exterior
           [URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-1.jpg
      )

      [3] => Array
      (
      )

      [3_attr] => Array
      (
           [Type] => Recreational Facilities
           [URL] => http://images.gta-travel.com/HH/Images/SA/ELS/ELS-HOL1-12.jpg
      )

     )
)

如何将所有_attr键值存储在单独的数组中。

1 个答案:

答案 0 :(得分:0)

// this may cause a dereference error.. if it does just run array_keys
// and stro it in a sperate var that is then passed to preg_grep
$attrKeys = preg_grep('#\d+_attr#', array_keys($xmlArray));
$attrKeys = array_flip($attrKeys);
$attributes = array_intersect_key($xmlArray, $attrKeys);
$tags = array_diff_key($xmlArray, $attributes);