-3

I have this value

$result = '<?xml version="1.0" encoding="UTF-8"?> <PrintLetterBarcodeData uid="123" name="Demo Name" gender="M" yob="2000" gname="Abdul Mannan" lm="city" vtc="City2" po="norway" dist="california" subdist="Ny" state="US" pc="12345" dob="22/06/2000"/>';

and i want the uid,name,gneder,yob,gname, lm Value and so on.

sheplu
  • 2,937
  • 3
  • 24
  • 21
  • 2
    _I have this value....i want the uid..._ What have you tried so far? – B001ᛦ Oct 10 '17 at 10:15
  • https://stackoverflow.com/questions/6578832/how-to-convert-xml-into-array-in-php Explains it very well – Doomenik Oct 10 '17 at 10:15
  • `simplexml_load_string($result);`, then attributes can be accessed using `['uid']` for example. – Nigel Ren Oct 10 '17 at 10:24
  • 1
    what's the status of this question? there's an answer below which to me looks like a possible solution. You didn't comment and/or accepted it, given if it was the solution for this. – Funk Forty Niner Dec 07 '17 at 17:35

1 Answers1

0

I've never used simplexml_load_string() before, so I thought I'd take the opportunity to self-educate.

After a combination of reading Doomenik's posted link, reading the manual on simplexml_load_string(), and running some tests on 3v4l.org, this seems to be a direct/advisable solution:

Code: (Demo)

$result='<?xml version="1.0" encoding="UTF-8"?> <PrintLetterBarcodeData uid="123" name="Demo Name" gender="M" yob="2000" gname="Abdul Mannan" lm="city" vtc="City2" po="norway" dist="california" subdist="Ny" state="US" pc="12345" dob="22/06/2000"/>';
$array=json_decode(json_encode(simplexml_load_string($result)),true);
foreach($array['@attributes'] as $k=>$v){
    echo "<div>$k : $v</div>\n";
}

Output:

<div>uid : 123</div>
<div>name : Demo Name</div>
<div>gender : M</div>
<div>yob : 2000</div>
<div>gname : Abdul Mannan</div>
<div>lm : city</div>
<div>vtc : City2</div>
<div>po : norway</div>
<div>dist : california</div>
<div>subdist : Ny</div>
<div>state : US</div>
<div>pc : 12345</div>
<div>dob : 22/06/2000</div>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136