2015-09-29 42 views
0

我试图将每个<outfit>Subnodes</outfit>子节点添加到使用php的数组中。添加xml子数组php

我的实际代码:

public static function get_outfits($username) { 
    $files = self::user_files($username); 
    $data = self::request($files['outfits']); 
    $data = @simplexml_load_string($data); 
    if ($data && count(@$data->xpath('//outfits/outfit')) > 0) { 
     foreach(@$data->xpath('//outfits/outfit/*') as $items) { 
     $response['outfits']['items'][] = array(
      "url"   => (string)$items['url'], 
      "c"   => (string)$items['c'], 
      "c2"   => (string)$items['c2'], 
      "displayName" => (string)$items['displayName'], 
      "z"   => (string)$items['z'], 
      "id"   => (string)$items['id'], 
      "isUgc"  => (string)$items['isUgc'] 
     ); 
     } 
    } 
    return (isset($response) ? $response : false); 
} 

XML文档如下所示:http://outfits.zwinky.com/users/220/287/_perverted/outfits.xml

可悲的代码是每一个现有的子保存到一个数组中。但我试图为每个<outfit></outfit>节点创建一个数组索引,它应该包含子元素。

例如:

数组[0] =第一<outfit><outfit>

阵列之间一切[1] =第二<outfit><outfit>

之间一切没有人有一个想法,如何创建这个?

+1

您可以通过删除所有'@'错误沉默开始,检查每一个语句的任何错误。显示任何你发现的东西可能会告诉你什么是错的,没有任何我们的帮助! – RiggsFolly

+0

静音与类中的其他代码有关。它的工作原理,但它不工作,因为我想它的工作。该代码确实返回任何子元素。但可悲的是,每个孩子都在阵列中。这实际上是我想要的:数组[0] =第 阵列之间的一切[1] =介于两者之间的第二 d4nex

+0

_IT的作品,但它不是工作,我希望它WORK_ ** =它不起作用** – RiggsFolly

回答

0

需要两个环,一个用于outfit元素一个用于内部item元素。另一方面,你不需要这个条件,因为迭代一个空的数组/节点列表工作正常。

$outfits = new SimpleXmlElement($xml); 

$result = []; 
foreach ($outfits->xpath('outfit') as $outfit) { 
    $items = []; 
    foreach ($outfit->xpath('*') as $item) { 
    $items[$item->getName()] = [ 
     "url" => (string)$item['url'], 
     "c" => (string)$item['c'], 
     "c2" => (string)$item['c2'], 
     "displayName" => (string)$item['displayName'], 
     "z" => (string)$item['z'], 
     "id" => (string)$item['id'], 
     "isUgc" => (string)$item['isUgc'] 
    ]; 
    } 
    $result[] = [ 
    'name' => (string)$outfit['name'], 
    'items' => $items 
    ]; 
} 

var_dump($result); 

输出:

array(14) { 
    [0]=> 
    array(2) { 
    ["name"]=> 
    string(6) "babe13" 
    ["items"]=> 
    array(14) { 
     ["head"]=> 
     array(7) { 
     ["url"]=> 
     string(51) "http://.../assets/babe/heads/01/head1" 
     ["c"]=> 
     string(8) "0xF4C4A4" 
     ["c2"]=> 
     string(8) "0xffffff" 
     ["displayName"]=> 
     string(0) "" 
     ["z"]=> 
     string(5) "33000" 
     ["id"]=> 
     string(0) "" 
     ["isUgc"]=> 
     string(0) "" 
     } 
     ["face"]=> 
     array(7) { 
     ["url"]=> 
     string(50) "http://.../assets/babe/faces/01/grl1" 
     ["c"]=> 
     string(3) "0x0" 
     ["c2"]=> 
     string(8) "0xFF0000" 
     ["displayName"]=> 
     string(5) "girl1" 
     ["z"]=> 
     string(5) "34000" 
     ["id"]=> 
     string(8) "20014794" 
     ["isUgc"]=> 
     string(0) "" 
     } 
     ... 
0

如果这是你想要的输出: -

Array 
(
    [0] => Array 
     (
      [url] => http://assets.zwinky.com/assets/babe/heads/01/head1 
      [c] => 0xF4C4A4 
      [c2] => 0xffffff 
      [displayName] => 
      [z] => 33000 
      [id] => 
      [isUgc] => 
     ) 

    [1] => Array 
     (
      [url] => http://assets.zwinky.com/assets/babe/faces/01/grl1 
      [c] => 0x0 
      [c2] => 0xFF0000 
      [displayName] => girl1 
      [z] => 34000 
      [id] => 20014794 
      [isUgc] => 
     ) 

    [2] => Array 
     (
      [url] => http://assets.zwinky.com/assets/babe/midsections/01/ms1 
      [c] => 0xBCE4FE 
      [c2] => 
      [displayName] => 
      [z] => 9000 
      [id] => 
      [isUgc] => 
     ) 

... etc etc 

然后,所有你需要做的是修改代码,这

public static function get_outfits($username) { 
    $files = self::user_files($username); 
    $data = self::request($files['outfits']); 
    $data = @simplexml_load_string($data); 
    if ($data && count(@$data->xpath('//outfits/outfit')) > 0) { 
     foreach(@$data->xpath('//outfits/outfit/*') as $items) { 

     // $response['outfits']['items'][] = array(

     $response[] = array(
      "url"   => (string)$items['url'], 
      "c"   => (string)$items['c'], 
      "c2"   => (string)$items['c2'], 
      "displayName" => (string)$items['displayName'], 
      "z"   => (string)$items['z'], 
      "id"   => (string)$items['id'], 
      "isUgc"  => (string)$items['isUgc'] 
     ); 
     } 
    } 
    return (isset($response) ? $response : false); 
} 
+0

是的,但我需要一个数组索引每个 d4nex

+0

每个手段我需要在包含子元素的数组内的索引 – d4nex