I got the following object in PHP:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[native] => France
)
[0] => France
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[native] => Nederland
)
[0] => Netherlands
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[native] => Deutschland
)
[0] => Germany
)
)
How do I get only the values sitting right after [0]?
For the given example I would like to have: France Netherlands Germany
To get string value of SimpleXMLElement Object cast object to string explicitly:
$xmlObject = ...; // your object here
$str = (string) $xmlObject;
echo $str;
// also if you just
echo $xmlObject;
// $xmlObject will be casted to string implicitly
Related
againAnotherChild is an object now how can i put the data from it to an array.
foreach($anotherChild->children() as $againAnotherChild) //child to
//childchildchild
{
// echo "Inside Again child Tag attributes<br>";
$againAnotherChildArray[] = $againAnotherChild->getName();
//print_r($againAnotherChild);
// foreach($this->$againAnotherChild[0] as $Storage)
// {
// $store = $Storage;
// //echo $store;
// }
echo $againAnotherChild[0]."<br>";
//echo "Storage".$store;
}
if i do print_r($againAnotherChild) this is what i get which updates after each iteration
SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object (
[0] => uint8 ) SimpleXMLElement Object ( [0] => enum )
SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0]
=> enum ) SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => firmware ) SimpleXMLElement Object ( [0] => enum )
SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0]
=> uint8 ) SimpleXMLElement Object ( [0] => enum )
------------------------Next Iteration--------------
SimpleXMLElement Object ( [0] => nodeid ) SimpleXMLElement Object (
[0] => uint8 ) SimpleXMLElement Object ( [0] => ipaddress )
SimpleXMLElement Object ( [0] => macaddress ) SimpleXMLElement Object
( [0] => enum ) SimpleXMLElement Object ( [0] => uint8 )
SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object (
[0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 )
SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object (
[0] => enum ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement
Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 )
SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object (
[0] => uint16 ) SimpleXMLElement Object ( [0] => enum )
How can i put these uint8,uint16 etc into an array that keeps updating till last iteration?
The simple solution was just to type cast an object to certain data type
$store = (array)againAnotherChildArray;
What i did in my code is this
$nodesWithValues = (array)$anotherChild->children();
foreach ($nodesWithValues as $key => $value)
{
//echo "$key : $value <br>";//CfgVer : uint8
var_dump($nodesWithValues);
}
problem solved by simple type casting :D
Are you asking about removing duplicates in array?
Would this help?
$againAnotherChildArray[$againAnotherChild->getName()] = $againAnotherChild->getName();
That should create something like hash map, eg set of tuples;
First array:
[VariationSpecificsSet] => SimpleXMLElement Object
(
[NameValueList] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Size
[Value] => Array
(
[0] => 5FT King Size
[1] => 4FT6 Double
)
)
[1] => SimpleXMLElement Object
(
[Name] => Main Colour
[Value] => Array
(
[0] => Brown
[1] => Black
)
)
)
)
Second Array:
[Variation] => SimpleXMLElement Object
(
[StartPrice] => 14.99
[Quantity] => 12
[VariationSpecifics] => SimpleXMLElement Object
(
[NameValueList] => SimpleXMLElement Object
(
[Name] => Size
[Value] => No.10-1M
)
)
)
examine above two arrays
i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array
how can i store them ...??
You can detect is by is_array().
$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
foreach($myVal as $item){
echo $item->Name.":".echo $item->Value;
}
} else {
echo $myVal->Name.":".echo $myVal->Value;
}
Did you tried using json_encode like below.
You can convert the object to array.
$array=json_decode(json_encode($object),true);
I'm trying to return the value of the typeName key inside this object (xml).
[geometricProperty] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[typeName] => localizacao // < - - - This is the value I need.
)
[Point] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => swrefVgetZredeVcollX757678785
[srs] => ut_cm
)
[coordinates] => 38871,739716
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[typeName] => limite
)
[Polygon] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ID] => swrefVgeometryV08945038
[srs] => utm_23_cm
)
[outerBoundaryIs] => SimpleXMLElement Object
(
[LinearRing] => SimpleXMLElement Object
(
[coordinates] => 318950,7399585 31981,39650 31826,73990 316956,79750
)
)
)
)
...
I've tried the following but it returns NULL
echo "<p>This object has ".(count($n->Feature->geometricProperty))." items.</p>";
$c=0;
foreach($n->Feature->geometricProperty as $k){
$c++;
echo "<p>$c)".(gettype($k))."</p>";
foreach(array_keys(get_object_vars($k)) as $o){
echo "<p>$o</p>";
switch($o){
case "#attributes": var_dump($k->{$o}["typeName"]); $tipo=$k->{$o}["typeName"]; break;
case "Point": $value=$k->{$o}->coordinates; break;
case "Polygon": $value=$k->{$o}->outerBoundaryIs->LinearRing->coordinates; break;
case "Line...": break;
}
}
echo "<p>TIPO: $tipo</p><p>VALOR: $value</p>";
}
OUTPUT:
This object has 3 items.
1)object
#attributes
NULL
Point
TIPO:
VALOR: 31869871,739796106
2)object
#attributes
NULL
Polygon
TIPO:
Search and found this overflow question and can't guess what am I doing wrong.
You can use SimpleXmlElement::attributes() to get an array of attributes. So then you would just loop over them and display them or use implode or something... depending on the output you want.
AI have an XML which have attributes as well as values in them. I want to convert it to an Array or Array Object along with attributes and values.
XML
<?xml version="1.0" encoding="UTF-8"?>
<itemBody>
<div label="options">
<optionchoices optionIdentifier="RESPONSE" shuffle="false" maxOptions="1">
<choice identifier="A1"><![CDATA[aaaa]]></choice>
<choice identifier="A2"><![CDATA[bbbb]]></choice>
<choice identifier="A3"><![CDATA[cccc]]></choice>
</optionchoices>
</div>
</itemBody>
I tried two set of code but the result was not as expected.
Code 1
<?php
$xml = simplexml_load_file('test.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => aaaa
[1] => bbbb
[2] => cccc
)
)
)
)
In the above output if we check then in choice node we get the values only and not the attributes
Code 2
<?php
$xml = simplexml_load_file('test.xml');
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A3
)
)
)
)
)
)
In this output we get only attributes of XML.
Now what I want is to obtain Attributes as well as Values of the XML.
Please help.
Thanks in advance.
This is what I got. And this is the solution which I expected.
http://outlandish.com/blog/xml-to-json/
I am using php to consume a web service (in coldfusion) to validate against the active directory. My code is below.
<?php
$racf = $_SERVER['AUTH_USER'];
//echo $racf;
//echo $myracf = trim($racf, "FEDERATED\.");
//get authenticated user
$arrUser = explode("\\", $_SERVER["LOGON_USER"]);
$racf = $arrUser[1];
echo $racf.'<br ><br >';
$logins = "http://acoldfusionwebservice/login.cfc?method=loginad&racf=$racf";
if( ! $xml = simplexml_load_file($logins) )
{
echo 'unable to load XML file';
}
else
{
//echo 'XML file loaded successfully <br />';
print_r ($xml);
}
?>
And this produces the following.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[header] => SimpleXMLElement Object
(
)
[data] => SimpleXMLElement Object
(
[recordset] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowCount] => 1
[fieldNames] => cn,mail,givenName,sn
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => cn
)
[string] => B000000
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => mail
)
[string] => john.doe#company.com
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => givenName
)
[string] => John
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => sn
)
[string] => Doe
)
)
)
)
)
Can someone help me to parse this information so that I can assign variables and use them. Thanks.
Try
$xml->data->recordset->field[0]->string
I think that field[0] is an Object again