How to store data in Object to an array in php - php

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;

Related

Get values from an XML object in PHP

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

how to show multidimentional array result using foreach loop in php

Array
(
[result] => Array
(
[0] => stdClass Object
(
[uniq_id] => 00fdc23c151ad0044d60
)
[1] => stdClass Object
(
[uniq_id] => 590dde424e
)
[2] => stdClass Object
(
[uniq_id] => 6f0eb3bb34
)
[3] => stdClass Object
(
[uniq_id] => eeb6c63929
)
[4] => stdClass Object
(
[uniq_id] => a72034387e
)
}
}
But I want only multiple uniq_id one by one in foreach loop
like
00fdc23c151ad0044d60
590dde424e
6f0eb3bb34
eeb6c63929
a72034387e
Its a stdClass Object array, so try this:
foreach($yourArray['result'] as $data)
{
echo $date->uniq_id.'<br>';
}
Note: stdClass Object properties can be accessed using ->

How to read xml into mysql db

I am pulling data from an external site using simplexml_load_file and need to dump into my local database. I do not know how to parse the data so that I can perform my sql insert. Please tell me how to grab the column headings and values from the simplexml_load_file results. Because I may not always know the heading names, Ideally I would like to pull the heading and value in as variables and load them both into sql as values to two columns; column_nam and column_value. Below is an example of the data usnig the print_r
SimpleXMLElement Object
(
[#attributes] => Array
(
[sessionid] => p_panda143ccfcf692ede95e|43413dc74e459ecb|41b5f89600000000|41c18afc4b000000|
)
[OBJECT] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => clinicalreport
[op] => search_filedelivery
)
[reports_viewed] => SimpleXMLElement Object
(
)
[reports_delivered] => SimpleXMLElement Object
(
)
[clinicalreport] => 3006163008
[orders_transmitted] => SimpleXMLElement Object
(
)
[report_request_date] => 10/7/2014 9:17 AM
[reports_undelivered] => SimpleXMLElement Object
(
)
[person_middle_name] => R
[reports_printed] => SimpleXMLElement Object
(
)
[creation_date] => SimpleXMLElement Object
(
)
[ownerid] => 3003154010
[reports_count] => SimpleXMLElement Object
(
)
[sponsor_name] => LabCorp Birmingham
[receivingorganization] => 3003154010
[organization_name] => SimpleXMLElement Object
(
)
[sponsor] => 1502182
[document] => SimpleXMLElement Object
(
)
[clearance] => 10
[report_subject] => HL7 Order Copy
[report_service_date] => 10/7/2014 9:19 AM
[reports_forwarded] => SimpleXMLElement Object
(
)
[forwarded] => SimpleXMLElement Object
(
)
[std_orders_created] => SimpleXMLElement Object
(
)
[person_last_name] => Angles
[is_viewed] => SimpleXMLElement Object
(
)
[is_latest] => SimpleXMLElement Object
(
)
[is_abnormal] => SimpleXMLElement Object
(
)
[expiration_date] => 4/5/2015 9:19 AM
[sequence] => SimpleXMLElement Object
(
)
[is_annotated] => SimpleXMLElement Object
(
)
[originalreport] => SimpleXMLElement Object
(
)
[person_account_number] => 1799
[filler_order_number] => SimpleXMLElement Object
(
)
[placer_order_number] => SimpleXMLElement Object
(
)
[is_forwarded] => SimpleXMLElement Object
(
)
[report_priority] => SimpleXMLElement Object
(
)
[person] => SimpleXMLElement Object
(
)
[receiving_cg_fname] => SimpleXMLElement Object
(
)
[creation_datetime] => 10/7/2014 9:19 AM
[receiving_cg_lname] => SimpleXMLElement Object
(
)
[is_downloaded] => N
[receiving_cg_mname] => SimpleXMLElement Object
(
)
[report_status] => NA
[lab_code] => MB
[ordering_cg_suffix] => SimpleXMLElement Object
(
)
[ordering_cg_fname] => SimpleXMLElement Object
(
)
[ordering_client_id] => 8001631
[person_first_name] => Madeleine
[report_type] => ORDER
[ordering_cg_lname] => SimpleXMLElement Object
(
)
[ordering_cg_mname] => SimpleXMLElement Object
(
)
[previousreport] => SimpleXMLElement Object
(
)
[orderresult] => SimpleXMLElement Object
(
)
[receiving_cg_suffix] => SimpleXMLElement Object
(
)
[psc_orders_created] => SimpleXMLElement Object
(
)
[receiving_client_id] => 8001631
[is_printed] => SimpleXMLElement Object
(
)
[content_expiration_date] => 2/4/2015 9:19 AM
[person_suffix] => SimpleXMLElement Object
(
)
[abnormals_count] => SimpleXMLElement Object
(
)
)
)
I found the answer. But first why is there a '-1' by my question.
SOLUTION - I needed to send the results of the simplexml_load_file in the new function xml2array. this function provided me with each name and value.
$myXML = simplexml_load_file($url);
function xml2array($myXML) {
$arr = array();
foreach ($myXML as $element) {
$tag = $element->getName();
$e = get_object_vars($element);
if (!empty($e)) {
$arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
}
else {
$arr[$tag] = trim($element);
}
}
return $arr;
}
I found the function here
http://bookofzeus.com/articles/convert-simplexml-object-into-php-array/

How to retrieve value from SimpleXMLElement Object

After calling the prestashop webservice, I recieved the response as shown below:
SimpleXMLElement Object
(
[order] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 2
)
)
)
)
I tried to retrive the content by looping as shown below:
foreach($resources as $resource){
echo '<pre>';
print_r($resource['id']);
echo '</pre>';
}
This gives me:
SimpleXMLElement Object
(
[0] => 1
)
SimpleXMLElement Object
(
[0] => 2
)
How can I retrieve 1 and 2 which are the values of these objects? thanks
I hate simplexml...
<?php
$xml = file_get_contents('xml.xml');
$xml = new SimpleXMLElement($xml);
foreach($xml as $key => $value)
{
$attrs = $value->attributes();
foreach($attrs as $attr_k => $attr_v)
echo $attr_k.": ".$attr_v."\n";
}

Parsing PHP SimpleXMLElement

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

Categories