2015-09-29 108 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