2016-04-11 23 views
0

Bir json dizisinden veri ayıklamaya çalışıyorum ancak her zaman boş bir dizim var.Bir JSON dizisinden veriyi ayıkla php

Benim XML: XML

<?xml version="1.0" encoding="UTF-8"?> 
<ItemList> 
     <Entry Item = "test1" Object = "1tset" /> 
     <Entry Item = "test2" Object = "2tset" /> 
</ItemList> 

Benim Dökümü:

array(1) { ["Entry"]=> array(2) 
{ [0]=> array(1) { ["@attributes"]=> array(2) { ["Item"]=> string(5) "test1" ["Object"]=> string(5) "1tset" } } 
[1]=> array(1) { ["@attributes"]=> array(2) { ["Item"]=> string(5) "test2" ["Object"]=> string(5) "2tset" } } } } 

Benim php kodu: Bütün "Öğe" veya "Object" dan almak gerekir

$xmlString = "my.xml"; 
$xmlObject = simplexml_load_file($xmlString); 
$xmltovar = json_decode(json_encode($xmlObject), true); 


foreach($xmltovar['Entry'] as $test) { 

    echo $test['Item']."<br>"; 
} 

ItemList-> Giriş Etiketi.

cevap

1

senin $xmltovar böyledir kodlama sonra ve şifre çözme:

Array 
(
    [Entry] => Array 
     (
      [0] => Array 
       (
        [@attributes] => Array 
         (
          [Item] => test1 
          [Object] => 1tset 
         ) 

       ) 

      [1] => Array 
       (
        [@attributes] => Array 
         (
          [Item] => test2 
          [Object] => 2tset 
         ) 

       ) 

     ) 

) 

Bunu yerine:

echo $test['Item']."<br>"; 

yazma: Aynı

echo $test['@attributes']['Item']."<br>"; 

Nesne için de geçerli.

0

Kişisel php bu

<?php 

$xmlString = "my.xml"; 
$xmlObject = simplexml_load_file($xmlString); 
$xmltovar = json_decode(json_encode($xmlObject), true); 


foreach($xmltovar['Entry'] as $test) { 
    echo $test["@attributes"]['Item']."<br>"; 
} 

?> 
0

$ testi gibi bakmak gerekir [ '@ nitelikleri'] [ 'Öğe'] Madde

$ testi için [ '@ niteliklerini'] [ 'Nesne'] Nesne için