how to parse stdclass - php

I have this data as a response:
stdClass Object
(
[GetReceiveMessagesResult] => stdClass Object
(
[Messages] => Array
(
[0] => stdClass Object
(
[MessageID] => 63012240
[RecipientNumber] => 30006708212212
[SenderNumber] => 09379580052
[Body] => Esm200aliaranbeigi
[ReceiveDate] => 1482389480
)
[1] => stdClass Object
(
[MessageID] => 63012231
[RecipientNumber] => 30006708212212
[SenderNumber] => 09379580052
[Body] => Esp243محسن قائدی
[ReceiveDate] => 1482389454
)
)
)
)
How I can print Messages items ?
$counter=count($results->GetReceiveMessagesResult->Messages);

You can use foreach loop
Try
foreach ($results->GetReceiveMessagesResult->Messages as $msg)
{
echo $msg->MessageID;
}

$counter=count($results->GetReceiveMessagesResult->Messages);
if($counter>0)
{
foreach ($results->GetReceiveMessagesResult->Messages as $msg)
{
echo $msg->MessageID;
echo $msg->RecipientNumber;
}
}

Related

Foreach - determine if key exists in each iteration

I have the following Array.
I am trying to figure out how to create an if statement in the foreach that will set true or false based on if any of the individual indexes has [Customer_Facing_Comments__r]
I am using this code - however, this just tells me if [Customer_Facing_Comments__r] is in the array - which it is, every time. I need to see if it is in each index [0], [1]..etc...and set true or false base on that.
CODE:
foreach($queryResult->records as $record){
if (array_key_exists("Customer_Facing_Comments__r",$record)) {
$test = 'true'; }
else { $test = 'false'; } }
QueryResult Object
(
[queryLocator] =>
[done] => 1
[records] => Array
(
[0] => stdClass Object
(
[Id] => 5003xx0255mhcAAA
[Account] => stdClass Object
(
[Id] => 0010cxx026IwsTAAS
[Name] => xxx
)
[AccountId] => 0010c0xxwsTAAS
[Customer_Facing_Comments__r] => stdClass Object
(
[done] => 1
[queryLocator] =>
[records] => Array
(
[0] => stdClass Object
(
[Id] =>
[Comment__c] => test string
[CreatedDate] => 2021-02-13T00:25:50.000Z
[Trouble_Ticket__c] => 5003x0000255mhcAAA
)
)
[size] => 1
)
[Type] => Service Down
)
[1] => stdClass Object
(
[Id] => 5003x000024Y6TpAAK
[Account] => stdClass Object
(
[Id] => 0010c0cccc6IwsTAAS
[Name] => test
)
[AccountId] => 0010c00cccIwsTAAS
)
Just expose the key in the foreach and use that in your result array $test:
foreach($queryResult->records as $key => $record){
if (isset($record->Customer_Facing_Comments__r)) {
$test[$key] = 'true';
} else {
$test[$key] = 'false';
}
}

PHP Soap Request foreach

I have a Souap Request and I do not know how I can go through all VoucherCodeItems with foreach.
I tried this, but it dosn't work:
foreach($response->VoucherCodeCollection->VoucherCodeItem AS $key => $val) {
echo "Feld $key hat den Wert: $val<br>";
}
This is what i get when make a print like:
print_r($response);
Result
stdClass Object
(
[TotalResults] => 2
[VoucherCodeCollection] => stdClass Object
(
[VoucherCodeItem] => Array
(
[0] => stdClass Object
(
[Id] => 215523
[ProgramId] => 6767
[Code] =>
[Title] => Adventskalender
[Description] => Im Adventskalender
)
[1] => stdClass Object
(
[Id] => 215453
[ProgramId] => 8476
[Code] =>
[Title] => Wir schenken dir 15 EUR!!
[Description] =>
)
)
)
)
You can't directly echo out the object:
Try:
foreach ($response->VoucherCodeCollection->VoucherCodeItem as $key => $val) {
echo "Feld $key hat den Wert: {$val->Title}<br>";
}

Foreach loop not display result in Codeigniter View

I'm trying get multiple records of attendance comment by studentIDs, and ClassID but when i print Array
Array ( [0] => Array ( [0] => stdClass Object ( [attendanceID] => 89 [comment] => This is Yusra ) ) [1] => Array ( [0] => stdClass Object ( [attendanceID] => 90 [comment] => this is zara butt ) ) [2] => Array ( [0] => stdClass Object ( [attendanceID] => 91 [comment] => this is ibrahim ) ) [3] => Array ( [0] => stdClass Object ( [attendanceID] => 92 [comment] => comment ) ) [4] => Array ( [0] => stdClass Object ( [attendanceID] => 93 [comment] => ) ) [5] => Array ( [0] => stdClass Object ( [attendanceID] => 94 [comment] => ) ) [6] => Array ( [0] => stdClass Object ( [attendanceID] => 95 [comment] => ) ) [7] => Array ( [0] => stdClass Object ( [attendanceID] => 96 [comment] => ) ) )
but i print in view using foreach loop nothing show.. don't know where i'm doing wrong
Controllor
$studentID = []; // Multiple Student ids
$comments = [];
$this->data['set'] = $id; //class id
$this->data['students'] = $this->student_m->get_order_by_student(array('classesID' => $id, 'schoolyearID' => $schoolyearID));
foreach($this->data['students'] as $key => $val){
$studentID[] = $val->studentID; //
}
foreach ($studentID as $student) {
$comments[] = $this->sattendance_m->get_comment($id,$student);
}
$this->data['comments']= $comments;
print_r($comments);
Model
function get_comment($id,$student) {
$this->db->select('attendanceID,comment');
$this->db->where('classesID', $id);
$this->db->where_in('studentID', $student);
$this->db->order_by($this->_primary_key,"desc");
$this->db->limit(1);
$this->db->from($this->_table_name);
$query=$this->db->get();
return $query->result();
}
View
<?php
if(count($comments)) {
foreach ($comments as $key => $row) {
echo $row->comment;
}
}?>
As you can see output of print_r you have multiple comments per student. So you need to do inner loop again
if(count($comments))
{
foreach ($comments as $key => $row) {
foreach ($row as $value) {
echo $value->comment;
}
}
}
this should be
<?php
$i=0;
if($comments)
{
foreach ($comments as $key => $row) {
echo $key[$i++]->row
}
}else{
echo "null";
}
?>

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

How to iterate through a nested stdClass? [duplicate]

This question already has answers here:
stdClass object and foreach loops
(5 answers)
Closed 4 months ago.
I have an object like this:
stdClass Object
(
[_count] => 10
[_start] => 0
[_total] => 37
[values] => Array
(
[0] => stdClass Object
(
[_key] => 50180
[group] => stdClass Object
(
[id] => 50180
[name] => CriticalChain
)
)
[1] => stdClass Object
(
[_key] => 2357895
[group] => stdClass Object
(
[id] => 2357895
[name] => Data Modeling
)
)
[2] => stdClass Object
(
[_key] => 1992105
[group] => stdClass Object
(
[id] => 1992105
[name] => SQL Server Users in Israel
)
)
[3] => stdClass Object
(
[_key] => 37988
[group] => stdClass Object
(
[id] => 37988
[name] => CDO/CIO/CTO Leadership Council
)
)
[4] => stdClass Object
(
[_key] => 4024801
[group] => stdClass Object
(
[id] => 4024801
[name] => BiT-HR, BI & IT Placement Agency
)
)
[5] => stdClass Object
(
[_key] => 37845
[group] => stdClass Object
(
[id] => 37845
[name] => Israel Technology Group
)
)
[6] => stdClass Object
(
[_key] => 51464
[group] => stdClass Object
(
[id] => 51464
[name] => Israel DBA's
)
)
[7] => stdClass Object
(
[_key] => 66097
[group] => stdClass Object
(
[id] => 66097
[name] => SQLDBA
)
)
[8] => stdClass Object
(
[_key] => 4462353
[group] => stdClass Object
(
[id] => 4462353
[name] => Israel High-Tech Group
)
)
[9] => stdClass Object
(
[_key] => 4203807
[group] => stdClass Object
(
[id] => 4203807
[name] => Microsoft Team Foundation Server
)
)
)
)
I need to get the id and name in an HTML table, but I seem to have a hard time iterating through this object. TIA. I understand that I need to get to the Values Array, and then to the group object, but I trip over the transitions between object and array and foreach vs index based iteration.
For example I tried this:
foreach ($res as $values) { print "\n"; print_r ($values); }
It iterates trough the object, but it also gives me useless
10 0 37
echo "<table>"
foreach ($object->values as $arr) {
foreach ($arr as $obj) {
$id = $obj->group->id;
$name = $obj->group->name;
$html = "<tr>";
$html .= "<td>Name : $name</td>";
$html .= "<td>Id : $id</td>";
$html .= "</tr>";
}
}
echo "</table>";
Since this is the top result in Google if you search for iterate over stdclass it may be helpful to answer the question in the title:
You can iterate overa stdclass simply by using foreach:
$user = new \stdClass();
$user->flag = 'red';
foreach ($user as $key => $value) {
// $key is `flag`
// $value is `red`
}
function objectToArray( $data )
{
if ( is_object( $data ) )
$d = get_object_vars( $data );
}
Convert the Object to array first like:
$results = objectToArray( $results );
and use
foreach( $results as result ){... ...}
I know it's an old post , but for sake of others:
when working with stdClass you should use Reflections:
$obj = new ReflectionObject($object);
$propeties = $obj->getProperties();
foreach($properties as $property) {
$name = $property->getName(); <-- this is the reflection class
$value = $object->$name; <--- $object is your original $object
here you need to handle the result (store in array etc)
}
foreach($res->values as $value) {
print_r($value);
}

Categories